GemBox.Spreadsheet

CellRange.Style Property

Gets or sets cell style (CellStyle) on one or more excel cells.

public override CellStyle Style {get; set;}

Remarks

Property set will set style of multiple cells or of a merged range.

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;

Exceptions

Exception TypeCondition
InvalidOperationExceptionThrown if property get is attempted on a cell range which is not merged.

See Also

CellRange Class | GemBox.Spreadsheet Namespace | Merged