Monday, 15 August 2011 07:57

Copy Worksheet in Excel in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

Excel copy function enables you not only copy worksheets within Excel workbook but also copy worksheets between Excel Workbooks. This section will introduce solutions to copy worksheets in Excel both within one Excel workbook and among workbooks via a .NET Excel component in C#, VB.NET. Besides, all the cell formats in the original Excel worksheets will be completely remained.

Spire.XLS for .NET which enjoys popularity among both enterprise and individual customers enables users to fast manipulate Excel files in C#, VB.NET. With this Excel library, you can copy quickly your Excel worksheets in both cases referred above. And the copied worksheets remain all the formats which looks just the same with original Excel worksheets. You can prove this from picture below:

Copy Excel Worksheet

Before starting your solution, please remember to download Spire.XLS for .NET and install it on your system. After adding the Spire.Xls dll, let us begin Excel copy tasks one by one. First let us see how to copy Excel worksheet within Excel workbook.

Copy Excel Worksheets within Excel Workbook in C#, VB.NET

Copy worksheet within Excel workbook is very easy. You can perform this task only by one line of key code: Worksheet.CopyFrom(Worksheet worksheet). Before copying the worksheets, please make sure that there are specified worksheets where you can copy to and name them. If not, please add them by this method: Worksheet Add(string name).

[C#]
using Spire.Xls;

namespace Copy_Worksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\copy worksheets.xls");
            Worksheet worksheet = workbook.Worksheets[0];
            //add worksheets and name them
            workbook.Worksheets.Add("copied sheet1");
            workbook.Worksheets.Add("copied sheet2");
            //copy worksheet to the new added worksheets
            workbook.Worksheets[1].CopyFrom(workbook.Worksheets[0]);
            workbook.Worksheets[2].CopyFrom(workbook.Worksheets[0]);
            workbook.SaveToFile(@"..\copy worksheets.xls");
            System.Diagnostics.Process.Start(@"..\copy worksheets.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace Copy_Worksheet
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("..\copy worksheets.xls")
            Dim worksheet As Worksheet = workbook.Worksheets(0)
            'add worksheets and name them
            workbook.Worksheets.Add("copied sheet1")
            workbook.Worksheets.Add("copied sheet2")
            'copy worksheet to the new added worksheets
            workbook.Worksheets(1).CopyFrom(workbook.Worksheets(0))
            workbook.Worksheets(2).CopyFrom(workbook.Worksheets(0))
            workbook.SaveToFile("..\copy worksheets.xls")
            System.Diagnostics.Process.Start("..\copy worksheets.xls")
        End Sub
    End Class
End Namespace

Copy Excel Worksheets between Excel Workbooks in C#, VB.NET

Actually, this solution is not harder compared with the solution above. The difference is that this time the main method is Worksheet. Clone(), while not Worksheet. CopyFrom(), please see it here:
Worksheet newsheet = (Worksheet)worksheet.Clone(worksheet.Parent)

[C#]
using Spire.Xls;

namespace Copy_Worksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\copysheet.xls");
            Worksheet worksheet = workbook.Worksheets[0];
            Worksheet newsheet = (Worksheet)worksheet.Clone(worksheet.Parent);
            workbook.Worksheets.Add(newsheet);
            workbook.SaveToFile(@"copy.xls");
            System.Diagnostics.Process.Start(@"copy.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace Copy_Worksheet
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("..\copysheet.xls")
            Dim worksheet As Worksheet = workbook.Worksheets(0)
            Dim newsheet As Worksheet = CType(worksheet.Clone(worksheet.Parent),Worksheet)
            workbook.Worksheets.Add(newsheet)
            workbook.SaveToFile("copy.xls")
            System.Diagnostics.Process.Start("copy.xls")
        End Sub
    End Class
End Namespace

Additional Info

  • tutorial_title: Copy Excel Worksheet
Last modified on Thursday, 10 July 2014 08:29

Related items (by tag)