Manage Worksheets

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:

  1. Create an object of the Worksheet class.
  2. 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.
  3. Obtain a worksheet reference.
  4. Perform work on the worksheets.
  5. 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.