Merge multiple excel files into one excel File

Source: Internet
Author: User
Good afternoon, everyone:

When the younger brother merged multiple excel files from the crystal mirror table into one excel file, he found the following problem:

The sheet and the table are duplicated, that is, the text of the sheet is overwritten. If I create an excel file separately, each file contains porn and text. There is no problem with using the same method (as shown below) (the text is separated ). How can you solve this problem. Multi-region !!!


Private void MergeExcelFiles (string fileName, System. Collections. ArrayList
Files)
{
Excel. Application exlApp = new Excel. ApplicationClass ();

ExlApp. Application. SheetsInNewWorkbook = 1;
Excel. Workbook excelWorkBook =
ExlApp. Workbooks. Add (Excel. XlWBATemplate. xlWBATWorksheet );
ExcelWorkBook = exlApp. Workbooks. Add (Excel. XlWBATemplate. xlWBATWorksheet );
Excel. Worksheet excelWorkSheet =
(Excel. Worksheet) excelWorkBook. ActiveSheet;
ExcelWorkBook. Saved = true;
ExcelWorkBook. SaveCopyAs (fileName );
ExcelWorkBook. Close (null, null, null );
ExcelWorkBook = null;
ExcelWorkSheet = null;

ExlApp. Application. Visible = false;
ExlApp. Application. AlertBeforeOverwriting = false;

Object missing = System. Reflection. Missing. Value;
Excel. Workbook wkBookDest;
Excel. Workbook wkBookOrign;

Try
{
WkBookDest = exlApp. Workbooks. Open (fileName,
Missing, false, missing,
Missing, true, missing,
Missing, missing );


For (int I = 0; I <files. Count; I ++)
{

String OrignfileName = files [I]. ToString ();

WkBookOrign = exlApp. Workbooks. Open (OrignfileName,
Missing, true, missing,
Missing, false, missing,
Missing, missing );


Int nOrignSheetsCount = wkBookOrign. Worksheets. Count;
Int nDestSheetsCount = wkBookDest. Worksheets. Count;

Excel. Worksheet wkSheetDest;

WkSheetDest = wkBookDest. Worksheets. Add (missing,
WkBookDest. Worksheets [nDestSheetsCount],
NOrignSheetsCount,
Excel. XlSheetType. xlWorksheet) as Excel. Worksheet;


For (int j = 1; j <= nOrignSheetsCount; j ++)
{
Excel. Worksheet wkSheetOrign = wkBookOrign. Worksheets [j]
Excel. Worksheet;

WkSheetDest = wkBookDest. Worksheets [nDestSheetsCount + j]
Excel. Worksheet;
WkSheetDest. Activate ();
WkSheetDest. Name = GetSheetName (OrignfileName );
Excel. Range range =
WkSheetOrign. Cells. SpecialCells (Excel. XlCellType. xlCellTypeLastCell, missing );

Int nRow = range. Row;
Int nColumn = range. Column;

Excel. Range rngOrign =
WkSheetOrign. get_Range (wkSheetOrign. Cells [1,1], wkSheetOrign. Cells [nRow,
NColumn]);

Excel. Range rngDest = wkSheetDest. get_Range (wkSheetDest. Cells [1, 1],
WkSheetDest. Cells [nRow, nColumn]);

RngOrign. Copy (missing );
WkSheetDest. Paste (rngDest, missing );
WkBookDest. Save ();
}
WkBookOrign. Close (false, null, null );
}

Excel. Worksheet temp = (Excel. Worksheet) (wkBookDest. Worksheets [1]);
Temp. Delete ();
Temp = null;

WkBookDest. Save ();
WkBookDest. Close (null, null, null );
}
Catch (Exception ex)
{
CommonModule. Functions. ShowErrorMessageBox (ex. Message, "Save response parameters ");
}
Finally
{
WkBookDest = null;
WkBookOrign = null;
ExlApp. Quit ();
ExlApp = null;

GC. Collect ();
}

CommonModule. Functions. DeleteTempFiles ();
}
Private string GetSheetName (string fullName)
{
String fileName = new System. IO. FileInfo (fullName). Name;
Int index = fileName. IndexOf (".");
String sheetName = fileName. Substring (0, index );
Return sheetName;
}

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.