C#/VB.NET: Copy Worksheets in Excel

Excel copy function enables you to not only copy worksheets within Excel workbook but also copy worksheets between different Excel workbooks. This article will introduce solutions to copy worksheets within one Excel workbook and among different workbooks via Spire.XLS for .NET in C#, VB.NET. Besides, all the cell formats in the original Excel worksheets will be completely remained.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

  • Package Manager
PM> Install-Package Spire.XLS

Copy Excel Worksheets within Excel Workbook

The following are the steps to duplicate worksheets within an Excel workbook.

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Add a new blank sheet to the workbook using WorksheetCollection.Add() method.
  • Copy the original worksheet to the new sheet using Worksheet.CopyFrom() method.
  • Use Workbook.SaveToFile() method to save the changes to another file.
  • C#
  • VB.NET
using Spire.Xls;

namespace CopyExcelworksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the sample Excel
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            //Add worksheet and set its name
            workbook.Worksheets.Add("Sheet1_Copy");
         
           //copy worksheet to the new added worksheets
           workbook.Worksheets[1].CopyFrom(workbook.Worksheets[0]);
          
            //Save the Excel workbook.
            workbook.SaveToFile("Duplicatesheet.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("Duplicatesheet.xlsx");

        }
    }
}
Imports Spire.Xls

Namespace CopyExcelworksheet
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Load the sample Excel
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("Sample.xlsx")
            'Add worksheet and set its name
            workbook.Worksheets.Add("Sheet1_Copy")
            'copy worksheet to the new added worksheets
            workbook.Worksheets(1).CopyFrom(workbook.Worksheets(0))
            'Save the Excel workbook.
            workbook.SaveToFile("Duplicatesheet.xlsx", ExcelVersion.Version2013)
            System.Diagnostics.Process.Start("Duplicatesheet.xlsx")
        End Sub
    End Class
End Namespace

C#/VB.NET: Copy Worksheets in Excel

Copy Excel Worksheets between Excel Workbooks

The following are the steps to duplicate worksheets within an Excel workbook.

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the first worksheet.
  • Load another Excel sample document
  • Add a new blank sheet to the second workbook using WorksheetCollection.Add() method.
  • Copy the original worksheet to the new sheet using Worksheet.CopyFrom() method.
  • Use Workbook.SaveToFile() method to save the changes to another file.
  • C#
  • VB.NET
using Spire.Xls;

namespace CopyExcelworksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the sample Excel and get the first worksheet
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            //Load the second Excel workbook
            Workbook workbook2 = new Workbook();
            workbook2.LoadFromFile("New.xlsx");
            //Add a new worksheet and set its name
            Worksheet targetWorksheet = workbook2.Worksheets.Add("added");
            //Copy the original worksheet to the new added worksheets
            targetWorksheet.CopyFrom(sheet);
            //Save the Excel workbook.
            workbook2.SaveToFile("CopySheetBetweenWorkbooks.xlsx", FileFormat.Version2013);
            System.Diagnostics.Process.Start("CopySheetBetweenWorkbooks.xlsx");

        }
    }
}
Imports Spire.Xls

Namespace CopyExcelworksheet
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Load the sample Excel and get the first worksheet
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("Sample.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Load the second Excel workbook
            Dim workbook2 As Workbook = New Workbook
            workbook2.LoadFromFile("New.xlsx")
            'Add a new worksheet and set its name
            Dim targetWorksheet As Worksheet = workbook2.Worksheets.Add("added")
            'Copy the original worksheet to the new added worksheets
            targetWorksheet.CopyFrom(sheet)
            'Save the Excel workbook.
            workbook2.SaveToFile("CopySheetBetweenWorkbooks.xlsx", FileFormat.Version2013)
            System.Diagnostics.Process.Start("CopySheetBetweenWorkbooks.xlsx")
        End Sub
    End Class
End Namespace

C#/VB.NET: Copy Worksheets in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.