Copy Excel Worksheets

The code example above shows how to use IronXL to duplicate and copy WorkSheets within and between Excel WorkBooks. Copy and paste sheets to and from other workbooks. Create duplicate sheets within the same workbook.

The CopySheet method is used to duplicate a worksheet within the same workbook or spreadsheet. The name of the new worksheet is required as a parameter.

Use the CopyTo method to duplicate a sheet to or from another workbook. A WorkBook is required as the first parameter and is followed by the name of the new worksheet.

You can download a file project from this link.

How to Copy Excel Worksheets in C#

  1. Install an Excel library to copy worksheets.
  2. Load an Excel workbook and create a new sheet.
  3. Use the CopyTo method to copy data to the new worksheet.
  4. Save the new Excel worksheet.

Below is a sample code demonstrating these steps:

// Import the IronXL namespace
using IronXL;

class ExcelExamples
{
    static void Main()
    {
        // Load an existing workbook from a path
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Get a worksheet from the workbook
        WorkSheet sheet = workbook.GetWorkSheet("SourceSheet");

        // Duplication within the same workbook
        sheet.CopySheet("DuplicatedSheet");

        // Create a new workbook to demonstrate copying between workbooks
        WorkBook newWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Use the CopyTo method to copy the sheet to another workbook
        sheet.CopyTo(newWorkbook, "CopiedSheet");

        // Save the new workbook with the copied sheet
        newWorkbook.SaveAs("new_example.xlsx");

        // Save changes to the original workbook if needed
        workbook.Save();
    }
}
// Import the IronXL namespace
using IronXL;

class ExcelExamples
{
    static void Main()
    {
        // Load an existing workbook from a path
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Get a worksheet from the workbook
        WorkSheet sheet = workbook.GetWorkSheet("SourceSheet");

        // Duplication within the same workbook
        sheet.CopySheet("DuplicatedSheet");

        // Create a new workbook to demonstrate copying between workbooks
        WorkBook newWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Use the CopyTo method to copy the sheet to another workbook
        sheet.CopyTo(newWorkbook, "CopiedSheet");

        // Save the new workbook with the copied sheet
        newWorkbook.SaveAs("new_example.xlsx");

        // Save changes to the original workbook if needed
        workbook.Save();
    }
}
' Import the IronXL namespace
Imports IronXL

Friend Class ExcelExamples
	Shared Sub Main()
		' Load an existing workbook from a path
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Get a worksheet from the workbook
		Dim sheet As WorkSheet = workbook.GetWorkSheet("SourceSheet")

		' Duplication within the same workbook
		sheet.CopySheet("DuplicatedSheet")

		' Create a new workbook to demonstrate copying between workbooks
		Dim newWorkbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

		' Use the CopyTo method to copy the sheet to another workbook
		sheet.CopyTo(newWorkbook, "CopiedSheet")

		' Save the new workbook with the copied sheet
		newWorkbook.SaveAs("new_example.xlsx")

		' Save changes to the original workbook if needed
		workbook.Save()
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Explanation

  • Loading a Workbook: We begin by loading an existing Excel file example.xlsx.
  • Selecting a Worksheet: We retrieve a worksheet named "SourceSheet" to perform the copy operation.
  • Copying within the Same Workbook: The CopySheet() method is used here to duplicate the worksheet within the same workbook. The new sheet will be named "DuplicatedSheet".
  • Creating a New Workbook: A new WorkBook is created which will be the destination for copying a sheet from the original workbook.
  • Copying Between Workbooks: The CopyTo() method allows us to copy "SourceSheet" to the newWorkbook, resulting in a duplicated sheet named "CopiedSheet".
  • Saving Workbooks: Finally, the new workbook must be saved as "new_example.xlsx", and any changes to the original workbook can also be saved.

This code demonstrates handling Excel operations using IronXL, making it easy to manage Excel sheets programmatically.