Articles in this section
Category / Section

Copy Excel worksheet to another workbook in C#, VB.NET

7 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can copy Excel worksheet to another workbook or within the same workbook.

Steps to copy Excel worksheet to another workbook, programmatically:

Step 1: Create a new C# console application project.

Create a new C# console application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package

Step 3: Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to copy an Excel worksheet from the source workbook to the destination workbook.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Add source and destination workbooks
    IWorkbook sourceWorkbook = application.Workbooks.Open("Source.xlsx");
    IWorkbook destinationWorkbook = application.Workbooks.Open("Destination.xlsx");
    
    //Copy Excel worksheet from source workbook to the destination workbook
    destinationWorkbook.Worksheets.AddCopy(sourceWorkbook.Worksheets[0]);
 
    //Save the file
    destinationWorkbook.Save();
 
    System.Diagnostics.Process.Start("Destination.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Add source and destination workbooks
    Dim sourceWorkbook As IWorkbook = application.Workbooks.Open("Source.xlsx")
    Dim destinationWorkbook As IWorkbook = application.Workbooks.Open("Destination.xlsx")
    
    'Copy Excel worksheet from source workbook to the destination workbook
    destinationWorkbook.Worksheets.AddCopy(sourceWorkbook.Worksheets(0))
 
    'Save the file
    destinationWorkbook.Save()
 
    System.Diagnostics.Process.Start("Destination.xlsx")
End Using

 

A complete Windows Forms working example of how to copy Excel worksheet to another workbook in C# and VB can be downloaded from Copy Excel worksheet to another workbook.zip.

AddCopy() method adds a copy of the specified worksheet or worksheets to the worksheet collection. ExcelWorksheetCopyFlags enumeration can be used to specify options when copying worksheets. AddCopy() method has six overloads whose API references are as follows:

  1. AddCopy(Int32)
  2. AddCopy(Int32,ExcelWorksheetCopyFlags)
  3. AddCopy(IWorksheet)
  4. AddCopy(IWorksheet,ExcelWorksheetCopyFlags)
  5. AddCopy(IWorksheets)
  6. AddCopy(IWorksheets,ExcelWorksheetCopyFlags)

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

How to copy and insert a chart in the same worksheet using C#,VB.NET?

How to Copy a Range from One Workbook to Another Workbook?

How to copy and paste evaluated values from one range to another using XlsIO?

How to copy a cell value instead of formula in Spreadsheet?

How to merge the exported worksheet in an existing workbook?

Feature comparison of Interop and XlsIO

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied