Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combine Specific Worksheets of Excel Files using C#
// Open Excel A file.
Workbook excelA = new Workbook("Excel A.xlsx");
// Open Excel B file.
Workbook excelB = new Workbook("Excel B.xlsx");
// Create destination Workbook.
Workbook destWorkbook = new Workbook();
// First worksheet is added by default to the Workbook. Add the second worksheet.
destWorkbook.Worksheets.Add();
// Copy the Sales worksheet of Excel A file to destination file.
destWorkbook.Worksheets[0].Copy(excelA.Worksheets["Sales"]);
// Copy the Sales worksheet of Excel B file to destination file.
destWorkbook.Worksheets[1].Copy(excelB.Worksheets["Sales"]);
// By default, the worksheet names are "Sheet1" and "Sheet2" respectively.
// Lets give them meaningful names.
destWorkbook.Worksheets[0].Name = excelA.FileName + " - Sales";
destWorkbook.Worksheets[1].Name = excelB.FileName + " - Sales";
// Save the destination file.
destWorkbook.Save("CombinedFile.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment