Last active
April 5, 2023 22:09
-
-
Save aspose-com-gists/7e9f7233ef97c85a88c76341dd8b4337 to your computer and use it in GitHub Desktop.
Copy or Move Worksheets in Excel using C#
This file contains hidden or 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 source Excel file | |
Workbook sourceWorkbook = new Workbook("source.xlsx"); | |
// Open destination Excel file | |
Workbook destinationWorkbook = new Workbook("destination.xlsx"); | |
// Copy the first sheet of the source workbook into destination workbook | |
destinationWorkbook.Worksheets[0].Copy(sourceWorkbook.Worksheets[0]); | |
// Save the Excel file | |
destinationWorkbook.Save("copy-worksheets.xlsx"); |
This file contains hidden or 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 an existing Excel file | |
Workbook wb = new Workbook("workbook.xlsx"); | |
// Create a WorksheetCollection object with reference to the sheets of the Workbook | |
WorksheetCollection sheets = wb.Worksheets; | |
// Copy data to a new sheet from an existing sheet within the Workbook | |
sheets.AddCopy("Sheet1"); | |
// Save the Excel file | |
wb.Save("CopyWithinWorkbook.xlsx"); |
This file contains hidden or 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 an existing excel file | |
Workbook wb = new Workbook("workbook.xlsx"); | |
// Create a WorksheetCollection object with reference to the sheets of the Workbook | |
WorksheetCollection sheets = wb.Worksheets; | |
// Get the first worksheet. | |
Worksheet worksheet = sheets[0]; | |
// Move the first sheet to the third position in the workbook | |
worksheet.MoveTo(2); | |
// Save the Excel file | |
wb.Save("move-worksheet.xlsx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment