Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 5, 2023 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/7e9f7233ef97c85a88c76341dd8b4337 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7e9f7233ef97c85a88c76341dd8b4337 to your computer and use it in GitHub Desktop.
Copy or Move Worksheets in Excel using C#
// 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");
// 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");
// 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