Accessing Cells of a Worksheet

Accessing Cells

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 Cells collection that represents all cells in the worksheet.

We can use the Cells collection to access cells in a worksheet. Aspose.Cells provides three basic approaches to access cells in a worksheet:

  1. Using the cell name.
  2. Using a cell’s row and column index.
  3. Using a cell index in the Cells collection.

Using Cell Name

Developers can access any specific cell by passing its cell name to the Cells collection of the Worksheet class as an index.

If you create a blank worksheet at the start, the count of the Cells collection is zero. When you use this approach to access a cell, it checks whether the cell exists in the collection. If it does, the method returns the existing cell object; otherwise, it creates a new Cell object, adds it to the Cells collection, and then returns that object. This approach is the easiest way to access a cell if you are familiar with Microsoft Excel, but it is the slowest compared with the other approaches.

Using Row & Column Index of the Cell

Developers can access any specific cell by passing the indices of its row and column to the Cells collection of the Worksheet class. This approach works in the same way as the first approach.

Accessing the Maximum Display Range of a Worksheet

Aspose.Cells allows developers to access a worksheet’s maximum display range. The maximum display range—the range of cells between the first and last cell with content—is useful when you need to copy, select, or display the entire contents of a worksheet in an image.

You can access a worksheet’s maximum display range using the MaxDisplayRange method of the Cells collection.