How to Copy Excel Worksheet to Another Workbook in WinForms?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can copy an 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.
The 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. The AddCopy() method has six overloads whose API references are as follows:
- AddCopy(Int32)
- AddCopy(Int32,ExcelWorksheetCopyFlags)
- AddCopy(IWorksheet)
- AddCopy(IWorksheet,ExcelWorksheetCopyFlags)
- AddCopy(IWorksheets)
- 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
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 about how to Copy Excel Worksheet to Another Workbook in WinForms.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!