Manage Worksheets
Contents
[
Hide
]
Developers can easily create and manage worksheets in Microsoft Excel files programmatically using Aspose.Cells flexible API. This topic describes approaches for adding and removing worksheets in Microsoft Excel files.
Aspose.Cells provides a class Workbook that represents an Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file.
A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of methods for managing worksheets.
Adding Worksheets to a New Excel File
To create a new Excel file programmatically:
- Create an object of the Worksheet class.
- Call the Add method of the WorksheetCollection collection. An empty worksheet is added to the Excel file automatically. It can be referenced by passing the sheet index of the new worksheet to the WorksheetCollection collection.
- Obtain a worksheet reference.
- Perform work on the worksheets.
- Save the new Excel file with new worksheets by calling the Workbook class Save method.
Accessing Worksheets using Sheet Index
The following sample code shows how to access or get any worksheet by specifying its index.
Removing Worksheets using Sheet Index
Removing worksheets by name works well when the name of the worksheet is known. If you don’t know the worksheet’s name, use an overloaded version of the RemoveAt method that takes the sheet index of the worksheet instead of its sheet name.