Accessing Cells and Ranges with ExpertXls spreasheet library for .NET |
A worksheet can be viewed as a collection of cells or ranges of cells. With ExpertXls Excel Library for .NET you can address a cell or a range of cells using the A1 string notation or using the row and column indexe numbers (the row and column indexes are one-based indexes). For example the, the cell from the top left corner of the worksheet (the cell from the first column of the first row of the worksheet), can be referenced by the "A1" string using the A1 notation or by the (1,1) row and column pair.
Similarly, a range of cells can be referenced in A1 notation by specifying the top left cell and the bottom row cell separated by a ':' character and it can be referenced with column and row indexes by specifying the row and column indexes of the top left cell and the bottom row cell.
Accessing spreadsheet cells and ranges examples:
// the top left cell of the worksheet in A1 notation ExcelRange firstCell = worksheet["A1"]; // the top left cell of the worksheet referenced by row and columns indexes ExcelRange firstCell = worksheet[1,1]; // a range of 4 cells from the top left corner of the worksheet in A1 notation ExcelRange firstRange = worksheet["A1:B2"]; // a range of 4 cells from the top left corner of the worksheet referenced by row and column indexes ExcelRange firstRange = worksheet[1, 1, 2, 2];
The ExpertXls Excel Library for .NET offers also the possibility to assign a name for a range of a worksheet. Further, that range can be referenced by its assigned name in formulas or when accessing that range instead of providing its full A1 reference string or its row and column indexes. A named range is represented by a ExcelNamedRange object in the library and it can be defined at worksheet or at workbook level. A named range defined at worksheet level can be referenced only from the worksheet where it was defined while a named range defined at workbook level can accessed from any worksheet of the workbook.
The code below creates two named ranges, the first at workbook level and the second at worksheet level: