Accessing Cells of a Worksheet

Accessing Cells

Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection 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 the cells in the worksheet.

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

  1. Using cell name.
  2. Using row & column index.

Using Cell Name

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

If you create a blank worksheet at the start, the count of Cells collection is zero. When you use this approach to access a cell, it will check whether this cell exists in the collection or not. If yes, it returns the cell object in the collection otherwise, it creates a new Cell object, adds the object to the Cells collection and then returns the object. This approach is the easiest way to access the cell if you are familiar with Microsoft Excel but it’s slower than other approaches.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UsingCellName.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the worksheet in the Excel file
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0);
com.aspose.cells.Cells cells = worksheet.getCells();
// Accessing a cell using its name
com.aspose.cells.Cell cell = cells.get("A1");
// Print message
System.out.println("Cell Value: " + cell.getValue());

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 that of the first approach.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UsingRowAndColumnIndexOfCell.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the worksheet in the Excel file
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0);
com.aspose.cells.Cells cells = worksheet.getCells();
// Accessing a cell using the indices of its row and column
com.aspose.cells.Cell cell = cells.get(0, 0);
// Print message
System.out.println("Cell Value: " + cell.getValue());

Accessing Maximum Display Range of 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 Worksheet.getCells().getMaxDisplayRange().

In the following figure, the selected worksheet' maximum display range is A1:G15.

Showing the maximum display range of this worksheet

todo:image_alt_text

The following sample code illustrates how to access the MaxDisplayRange property. The code generates the following output.

 Maximum Display Range: =Sheet1!$A$1:$G$15

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getSharedDataDir(AccessingMaximumDisplayRangeofWorksheet.class) + "data/";
// Instantiate a workbook from source file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Access the first workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access the Maximum Display Range
Range range = worksheet.getCells().getMaxDisplayRange();
// Print the Maximum Display Range RefersTo property
System.out.println("Maximum Display Range: " + range.getRefersTo());