C#處理多個Excel合并,複製Sheet

來源:互聯網
上載者:User

項目中需要把多個Excel報表進行合并, 用一個Excel的多個Sheet來表現,合并報表代碼如下:  

 

       private void btnExport_Click(object sender, EventArgs e)

        {            string fileName = "";            System.Windows.Forms.FileDialog dlg = null;            dlg = new SaveFileDialog();            //根據格式設定檔案字串            dlg.Filter = this.GetFileFilterString();            //開啟檔案            if (dlg.ShowDialog() == DialogResult.OK)            {                fileName = dlg.FileName;            }            if (dlg.FileName.Equals(""))            {                return;            }            //產生報表            this.Export(fileName);            string dir = fileName.Substring(0, fileName.LastIndexOf("//"));                        //報表合并            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();            Microsoft.Office.Interop.Excel.Workbook workbook1 = excel.Workbooks.Open(dir + "//file0.xls",                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                   Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Workbook workbook2 = excel.Workbooks.Open(dir + "//file1.xls",                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Workbook workbook3 = excel.Workbooks.Open(dir + "//file2.xls",                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                     Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Workbook workbook4 = excel.Workbooks.Open(dir + "//file3.xls",                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Workbook workbook5 = excel.Workbooks.Open(dir + "//file4.xls",                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Workbook workbook6 = excel.Workbooks.Open(dir + "//file5.xls",                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,                    Type.Missing, Type.Missing);            Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook1.Sheets["Sheet1"];            Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook2.Sheets["Sheet1"];            Microsoft.Office.Interop.Excel.Worksheet worksheet3 = (Microsoft.Office.Interop.Excel.Worksheet)workbook3.Sheets["Sheet1"];            Microsoft.Office.Interop.Excel.Worksheet worksheet4 = (Microsoft.Office.Interop.Excel.Worksheet)workbook4.Sheets["Sheet1"];            Microsoft.Office.Interop.Excel.Worksheet worksheet5 = (Microsoft.Office.Interop.Excel.Worksheet)workbook5.Sheets["Sheet1"];            Microsoft.Office.Interop.Excel.Worksheet worksheet6 = (Microsoft.Office.Interop.Excel.Worksheet)workbook6.Sheets["Sheet1"];            //設定報表Sheet Name            worksheet1.Name = "即時資產資訊";            worksheet2.Name = "即時持倉清單";            worksheet3.Name = "資產資訊";            worksheet4.Name = "證券餘額";            worksheet5.Name = "資金流水匯總";            worksheet6.Name = "資金流水匯總1";            //合并報表            worksheet6.Copy(Missing.Value, worksheet1);            worksheet5.Copy(Missing.Value, worksheet1);            worksheet4.Copy(Missing.Value, worksheet1);            worksheet3.Copy(Missing.Value, worksheet1);            worksheet2.Copy(Missing.Value, worksheet1);                   workbook1.Save();            workbook1.Close(false, Type.Missing, Type.Missing);                       workbook2.Close(false, Type.Missing, Type.Missing);            workbook3.Close(false, Type.Missing, Type.Missing);            workbook4.Close(false, Type.Missing, Type.Missing);            workbook5.Close(false, Type.Missing, Type.Missing);            workbook6.Close(false, Type.Missing, Type.Missing);            //如果報表檔案存在,先刪除            if (File.Exists(fileName))            {                File.Delete(fileName);            }            File.Copy(dir + "//file0.xls", fileName);            //刪除臨時檔案            for (int i = 0; i < 6; i++)            {                File.Delete(dir + "//file" + i + ".xls");            }        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.