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 to access 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 Cells collection to access cells in a worksheet. Aspose.Cells provides three basic approaches to access cells in a worksheet:

  1. Using 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 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 that object. This approach is the easiest way to access the cell if you are familiar with Microsoft Excel but it’s the slowest one as compared to other approaches.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Accessing a cell using its name
Cell cell = cells.Get(u"B3");
//Write string value of the cell on console
std::cout << "Value of cell B3: "
<< cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

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.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Accessing cell B3 using its row and column index
Cell cell = cells.Get(2, 1);
//Write string value of the cell on console
std::cout << "Value of cell B3: "
<< cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

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 MaxDisplayRange method of the Cells collection.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Access the Maximum Display Range
Range range = cells.GetMaxDisplayRange();
//Print string value of the cell on console
std::cout << "Maximum Display Range of Worksheet: "
<< range.GetRefersTo().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();