Пример #1
0
        /// <summary>
        /// 将Excel文件另存为指定格式
        /// </summary>
        /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
        public void SaveAsFile(SaveAsFileFormat format)
        {
            if (this.outputFile == null)
            {
                throw new Exception("没有指定输出文件路径!");
            }

            try
            {
                switch (format)
                {
                case SaveAsFileFormat.HTML:
                {
                    workBook.SaveAs(outputFile, Excel.XlFileFormat.xlHtml, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                    break;
                }

                case SaveAsFileFormat.CSV:
                {
                    workBook.SaveAs(outputFile, Excel.XlFileFormat.xlCSV, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                    break;
                }

                case SaveAsFileFormat.TEXT:
                {
                    workBook.SaveAs(outputFile, Excel.XlFileFormat.xlUnicodeText, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                    break;
                }

                case SaveAsFileFormat.XML:
                {
                    workBook.SaveAs(outputFile, Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
                                    Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    break;
                }

                default:
                {
                    workBook.SaveAs(outputFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                    break;
                }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                this.Quit();
            }
        }
        private void button12_Click(object sender, EventArgs e)
        {
            string           filetype   = comboBox1.Text;
            string           extensions = string.Empty;
            SaveAsFileFormat format     = SaveAsFileFormat.EXCEL;

            if (filetype == "")
            {
                MessageBox.Show("请选择一项!");
                return;
            }

            switch (filetype)
            {
            case "EXCEL":
                extensions = "xls";
                format     = SaveAsFileFormat.EXCEL;
                break;

            case "CSV":
                extensions = "csv";
                format     = SaveAsFileFormat.CSV;
                break;

            case "TEXT":
                extensions = "txt";
                format     = SaveAsFileFormat.TEXT;
                break;

            case "XML":
                extensions = "xml";
                format     = SaveAsFileFormat.XML;
                break;

            case "HTML":
                extensions = "html";
                format     = SaveAsFileFormat.HTML;
                break;
            }
            string filepath = _path + "导出文件." + extensions;

            _helper = new ExcelHelper(_path + "初始化数据.xls", filepath);
            _helper.SaveAsFile(format);
            MessageBox.Show("已导出文件!");
        }
Пример #3
0
        /// <summary>
        /// 将Excel文件另存为指定格式
        /// </summary>
        /// <param name="format">HTML,CSV,TEXT,EXCEL,XML</param>
        public void SaveAsFile(SaveAsFileFormat format)
        {
            if (this.outputFile == null)
                throw new Exception("没有指定输出文件路径!");

            try
            {
                switch (format)
                {
                    case SaveAsFileFormat.HTML:
                        {
                            workBook.SaveAs(outputFile, Excel.XlFileFormat.xlHtml, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                            break;
                        }
                    case SaveAsFileFormat.CSV:
                        {
                            workBook.SaveAs(outputFile, Excel.XlFileFormat.xlCSV, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                            break;
                        }
                    case SaveAsFileFormat.TEXT:
                        {
                            workBook.SaveAs(outputFile, Excel.XlFileFormat.xlUnicodeText, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                            break;
                        }
                    case SaveAsFileFormat.XML:
                        {
                            workBook.SaveAs(outputFile, Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
                             Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            break;
                        }
                    default:
                        {
                            workBook.SaveAs(outputFile, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                            break;
                        }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                this.Quit();
            }
        }