In certain cases, you may need to copy worksheets within an Excel workbook or from one workbook to another. On the other hand, moving worksheets to change their position within a workbook is also required in different scenarios. In order to perform the above-mentioned tasks programmatically, this article shows how to copy Excel worksheets within workbooks using C#. Furthermore, it also covers how to change the position of a worksheet in an Excel workbook.
- C# API to Copy Excel Worksheets
- Copy Worksheets within an Excel Workbook
- Copy Worksheets from One Workbook to Another
- Move Worksheets from One Position to Another
- Get a Free API License
C# API to Copy Excel Worksheets
In order to copy worksheets in Excel files, we’ll leverage the powerful spreadsheet manipulation capabilities of Aspose.Cells for .NET. The API provides you with a wide range of features to generate and process Excel files from within your .NET applications. You can either download the API’s binaries or install it using NuGet.
PM> Install-Package Aspose.Cells
Copy Excel Worksheets using C#
The following are the steps to copy an Excel worksheet within a workbook using C#.
- Load Excel file using Workbook class.
- Retrieve worksheets in a WorksheetCollection object from Workbook.Worksheets property.
- Use WorksheetCollection.AddCopy(string) method to copy the worksheet by providing its name.
- Save the updated workbook using Workbook.Save(string) method.
The following code sample shows how to copy an Excel worksheet in a workbook.
Copy Excel Worksheets from One Workbook to Another
Now, let’s have a look at how to copy a worksheet from one Excel workbook to another. The following are the steps to perform this operation.
- Create an object of the Workbook class and load the source Excel file.
- Create another object of the Workbook class and load the destination Excel file.
- Copy worksheet from source to destination workbook using destinationWorkbook.Worksheets[int].Copy(sourceWorkbook.Worksheets[int]) method.
- Save the destination workbook using Workbook.Save(string) method.
The following code sample shows how to copy a worksheet from one workbook to another in C#.
Move Worksheet from One Position to Another in C#
There could be the case when you need to rearrange the positions of the worksheets in a workbook. You can do it by moving the worksheets to the desired positions by specifying the index. The following are the steps to move an Excel worksheet from one position to another.
- Load Excel file using Workbook class.
- Retrieve worksheets in a WorksheetCollection object using Workbook.Worksheets property.
- Access the desired worksheet from worksheet collection into a Worksheet object.
- Use Worksheet.MoveTo(int) method to move the worksheet to the specified index.
- Save the updated workbook using Workbook.Save(string) method.
The following code sample shows how to move an Excel worksheet from one position to another using C#.
Get a Free API License
You can try and evaluate Aspose.Cells for .NET for free by requesting a temporary license.
Conclusion
In this article, you have learned how to copy Excel worksheets within a workbook or from one workbook to another using C#. Furthermore, you have seen how to move an Excel worksheet from one position to another within a workbook. You can explore other features of Aspose.Cells for .NET using the documentation. In case you would have any queries, feel free to post to our forum.