Created
January 14, 2021 06:05
-
-
Save conholdate-gists/b7d9bcd7a6fbef33a103cfb1c1619f3c to your computer and use it in GitHub Desktop.
Combine Specific Worksheets of Excel Files using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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