Click or drag to resize

Accessing Cells and Ranges with ExpertXls spreasheet library for .NET

A cell or a range of cells is an ExcelRange object. Get it from the worksheet indexer with an address in A1 notation ("A1", "B2:D5") or with one based row and column indexes. The top left cell of a worksheet is "A1", or row 1, column 1.

A value or a style set on a range applies to all the cells of the range.

Code Samples
// a cell in A1 notation and by row and column index
ExcelRange firstCell = worksheet["A1"];
ExcelRange sameCell = worksheet[1, 1];

// a range of 4 cells in A1 notation and by top row, left column, bottom row and right column
ExcelRange firstRange = worksheet["A1:B2"];
ExcelRange sameRange = worksheet[1, 1, 2, 2];

// the same value in every cell of the range
firstRange.Value = 10;

// merge cells: the merged area shows the value of its top left cell
worksheet["A4:D4"].Merge();
worksheet["A4"].Text = "A merged range";

// the used range covers all the cells that have a value or a style
string usedAddress = worksheet.UsedRange.Address;

// the cells of a range one by one
foreach (ExcelRange cell in worksheet["A1:B2"].Cells)
    Console.WriteLine(cell.Value);
Named Ranges

A named range (ExcelNamedRange) gives a name to a range. The name can be used instead of the address, in the worksheet indexer, in formulas and as the target of hyperlinks. A name defined in a worksheet is visible only in that worksheet; a name defined in the workbook is visible in all the worksheets.

// a name defined in the workbook and a name defined in the worksheet
workbook.NamedRanges.AddNamedRange(worksheet["A1:A10"], "Prices");
worksheet.NamedRanges.AddNamedRange(worksheet["B1"], "Total");

// use the names instead of the addresses
worksheet["Prices"].Value = 25.5;
worksheet["Total"].Formula = "=SUM(Prices)";
See Also