Articles in this section
Category / Section

How to Copy Excel Worksheet to Another Workbook in WinForms?

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.

WinForms XIsIO worksheet

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 WinForms Excel

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.


Conclusion

I hope you enjoyed learning on how to copy Excel worksheet to another workbook in WinForms.

You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!



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