- Code: Select all
//Create a string array from Excel file paths
using Spire.Xls;
string[] inputFiles = new string[] { @"L:\Excel\Set 1\Set 1\SET2.xlsx", @"L:\Excel\Set 1\Set 1\SET1.xlsx" };
//Initialize a new Workbook object
Workbook newWorkbook = new Workbook();
//Clear the default worksheets
newWorkbook.Worksheets.Clear();
//Initialize another temporary Workbook object
Workbook tempWorkbook = new Workbook();
//Loop through the string array
foreach (string file in inputFiles)
{
//Load the current workbook
tempWorkbook.LoadFromFile(file);
//Loop through the worksheets in the current workbook
foreach (Worksheet sheet in tempWorkbook.Worksheets)
{
//Copy each worksheet from the current workbook to the new workbook
newWorkbook.Worksheets.AddCopy(sheet, WorksheetCopyType.None);
}
}
//Save the new workbook to file
newWorkbook.SaveToFile("L:\\Excel\\Set 1\\Set 1\\MergeWorkbooks.xlsx", ExcelVersion.Version2016);
Source/Outcome :