Access Cells and Cell Ranges
- 5 minutes to read
Worksheet Indexing Rules
Columns and rows in a worksheet are indexed with zeros. The first column has the index of 0, and the first row has the index of 0.

Obtain a Cell
Use Worksheet.Cells to access an individual Cell. The Cells collection supports an A1-style reference and zero-based row and column indexes.
The following code snippet accesses cell B2 by reference and by its zero-based indexes:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\test_workbook.xlsx"));
Worksheet worksheet = workbook.Worksheets[0];
// Access cell B2 by its A1-style reference.
Cell cellByReference = worksheet.Cells["B2"];
// Access the same cell by zero-based row and column indexes.
Cell cellByIndex = worksheet.Cells[1, 1];
Obtain a Cell Range
Use the Worksheet string indexer to access a rectangular CellRange. Call Worksheet.GetRange to specify a range by its zero-based top-left cell, row count, and column count.
The following code snippet accesses the range B2:D5 in either of the ways listed here:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Access cells B2 through D5 by their A1-style range reference.
CellRange rangeByReference = workbook.Worksheets[0]["B2:D5"];
// Access the same four-row, three-column range by zero-based indexes.
CellRange rangeByIndex = workbook.Worksheets[0].GetRange(1, 1, 4, 3);
Use the following properties to get information about the bounds and size of a cell range. If the range contains multiple areas, these properties get the bounds and size of the rectangle that contains all areas:
- CellRange.TopRowIndex
- CellRange.LeftColumnIndex
- CellRange.BottomRowIndex
- CellRange.RightColumnIndex
- CellRange.RowCount
- CellRange.ColumnCount
The CellRange.CellCount property gets the number of cells in the range. If the range contains multiple areas, the CellCount property gets the total number of cells in all areas, not the number of cells in the rectangle that contains all areas.
Work with Multi-Area Ranges
A multi-area range contains multiple rectangular cell ranges (areas) on the same worksheet. In Microsoft Excel, you can create a multi-area range by selecting multiple ranges while you hold the Ctrl key.
You can call the following methods to create a multi-area range:
- CellRange.Union
- CellRange.Union(others) (combines the current range with multiple ranges)
- CellRange.Exclude
- CellRange.Intersect
Use the CellRange.Areas property to access individual areas. For a single rectangular range, the Areas collection contains the range itself.
The following code snippet displays row and column indexes of each area in a multi-area range and checks the Areas collection of a single rectangular range:
using System;
using DevExpress.Docs.Spreadsheet;
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Display the bounds of each area in a multi-area range.
CellRange multiAreaRange = worksheet["A1:B2"].Union(worksheet["D4:E6"]);
foreach (CellRange area in multiAreaRange.Areas)
Console.WriteLine($"Rows {area.TopRowIndex}-{area.BottomRowIndex}, columns {area.LeftColumnIndex}-{area.RightColumnIndex}");
The Worksheet string indexer also accepts a reference with multiple areas separated by commas (for example, worksheet["A1:B2,D4"]).
Call the CellRange.GetMinimumCover() method to get the rectangle that contains all areas as a single range.
Shift and Resize a Cell Range
Call the following methods to get a new cell range based on an existing range. These methods do not move or modify cell content:
| Method | Description |
|---|---|
| CellRange.Offset(rowOffset, columnOffset) | Returns a cell range shifted by the specified number of rows and columns. |
| CellRange.Resize(rowCount, columnCount) | Returns a cell range with the same top-left cell and the specified number of rows and columns. |
The following code snippet gets the D3:E4 range (the B2:C3 range shifted one row down and two columns to the right) and the B2:E4 range (the B2:C3 range resized to three rows and four columns):
var worksheet = workbook.Worksheets[0];
var range = worksheet["B2:C3"];
var shiftedRange = range.Offset(1, 2);
var resizedRange = range.Resize(3, 4);
Access Used Cells
Use the following members to find cells that contain data in a worksheet:
| Member | Description |
|---|---|
| Worksheet.GetDataRange() | Returns a cell range that contains all cells with values or formulas in the worksheet. |
| Worksheet.GetUsedRange() | Returns a cell range that contains all cells with values, formulas, or formatting settings in the worksheet. |
| CellRange.HasData | Gets whether the range contains cells with values or formulas. |
The following code snippet gets the range that contains worksheet data and checks whether column F contains values or formulas:
var worksheet = workbook.Worksheets[0];
var dataRange = worksheet.GetDataRange();
var columnHasData = worksheet["F:F"].HasData;
Format Cells and Ranges
Refer to the following help topic for more information on formatting cells and ranges: Format Cells and Ranges.