GemBox.Spreadsheet

CellRange Class

Cell range is a rectangular group of worksheet cells.

For a list of all members of this type, see CellRange Members.

System.Object
   GemBox.Spreadsheet.AbstractRange
      GemBox.Spreadsheet.CellRange

public class CellRange : AbstractRange, IEnumerable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

Cell range is determined by its top (FirstRowIndex), left (FirstColumnIndex), bottom (LastRowIndex) and right (LastColumnIndex) borders. This properties are read-only, so if you require different cell range use one of GetSubrange methods (GetSubrangeAbsolute, GetSubrangeRelative or GetSubrange). Specific cell can be accessed in a few ways, depending on IndexingMode. Cells in the range can be merged / unmerged by the use of Merged property.

Value property set will set value of multiple cells or of a merged range. Value property get has meaning only if range is merged; otherwise, exception is thrown.

Style property set will set style of multiple cells or of a merged range. Style property get has meaning only if range is merged; otherwise, exception is thrown.

Note that for Style property set on a cell range that is not merged, you can't use the following format:

[Visual Basic]
    Dim cr As CellRange = excelFile.Worksheets(0).Rows(1).Cells
    cr.Style.Rotation = 30
[C#]
    CellRange cr = excelFile.Worksheets[0].Rows[1].Cells;
    cr.Style.Rotation = 30;
because that would first call Style property get method and that will certainly fail because Style property get is defined only for a merged cell range.

Instead you can use two different code patterns, depending on whether you want to replace or combine the existing cell range styles with the new style.

If you want to replace cell style on every cell in a cell range use the following code:

[Visual Basic]
    Dim cr As CellRange = excelFile.Worksheets(0).Rows(1).Cells
    Dim style As CellStyle = New CellStyle()
    style.Rotation = 30
    cr.Style = style
[C#]
    CellRange cr = excelFile.Worksheets[0].Rows[1].Cells;
    CellStyle style = new CellStyle();
    style.Rotation = 30;
    cr.Style = style;

If you want to set cell style property on every cell in a cell range (other cell style property values will remain unchanged) use the following code:

[Visual Basic]
    Dim cell As ExcelCell
    For Each cell In excelFile.Worksheets(0).Rows(1).Cells
        cell.Style.Rotation = 30
    Next
[C#]
    foreach(ExcelCell cell in excelFile.Worksheets[0].Rows[1].Cells)
        cell.Style.Rotation = 30;

Example

Following code creates horizontal, vertical and rectangular cell ranges and demonstrates how indexing works different in different context. SetBorders method is used to mark outside borders of the rectangular range.

[Visual Basic]
    Dim cr As CellRange = excelFile.Worksheets(0).Rows(1).Cells

    cr(0).Value = cr.IndexingMode
    cr(3).Value = "D2"
    cr("B").Value = "B2"

    cr = excelFile.Worksheets(0).Columns(4).Cells

    cr(0).Value = cr.IndexingMode
    cr(2).Value = "E3"
    cr("5").Value = "E5"

    cr = excelFile.Worksheets(0).Cells.GetSubrange("F2", "J8")
    cr.SetBorders(MultipleBorders.Outside, Color.Navy, LineStyle.Dashed)

    cr("I7").Value = cr.IndexingMode
    cr(0, 0).Value = "F2"
    cr("G3").Value = "G3"
    cr(5).Value = "F3" ' Cell range width is 5 (F G H I J).
[C#]
    CellRange cr = excelFile.Worksheets[0].Rows[1].Cells;                

    cr[0].Value = cr.IndexingMode;
    cr[3].Value = "D2";
    cr["B"].Value = "B2";

    cr = excelFile.Worksheets[0].Columns[4].Cells;

    cr[0].Value = cr.IndexingMode;
    cr[2].Value = "E3";
    cr["5"].Value = "E5";

    cr = excelFile.Worksheets[0].Cells.GetSubrange("F2", "J8");
    cr.SetBorders(MultipleBorders.Outside, Color.Navy, LineStyle.Dashed);

    cr["I7"].Value = cr.IndexingMode;
    cr[0,0].Value = "F2";
    cr["G3"].Value = "G3";
    cr[5].Value = "F3"; // Cell range width is 5 (F G H I J).

Requirements

Namespace: GemBox.Spreadsheet

Assembly: GemBox.Spreadsheet (in GemBox.Spreadsheet.dll)

See Also

CellRange Members | GemBox.Spreadsheet Namespace