Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/b7d9bcd7a6fbef33a103cfb1c1619f3c to your computer and use it in GitHub Desktop.
Save conholdate-gists/b7d9bcd7a6fbef33a103cfb1c1619f3c to your computer and use it in GitHub Desktop.
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