C # Merge multiple Excel files and copy Sheet

Source: Internet
Author: User

In the project, you need to merge multiple EXCEL reports and use multiple sheets in one Excel file. The code for the consolidated report is as follows:

 

Private void btnexport_click (Object sender, eventargs E)

{String filename = ""; system. windows. forms. filedialog DLG = NULL; DLG = new savefiledialog (); // set the file string DLG according to the format. filter = This. getfilefilterstring (); // open the file if (DLG. showdialog () = dialogresult. OK) {filename = DLG. filename;} If (DLG. filename. equals ("") {return;} // generate the report this. export (filename); string dir = filename. substring (0, filename. lastindexof ("//"); // The report is merged into 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"]; // sets the Report Sheet Name worksheet1.name = "real-time asset information"; worksheet2.name = "real-time position list"; worksheet3.name = "asset information "; worksheet4.name = "securities balance"; worksheet5.name = "capital flow summary"; worksheet6.name = "Capital Flow Summary 1"; // consolidated report 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 the report file exists, delete if (file. exists (filename) {file. delete (filename);} file. copy (DIR + "// file0.xls", filename); // Delete the temporary file for (INT I = 0; I <6; I ++) {file. delete (DIR + "// file" + I + ". xls ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.