Back to Devexpress

How to: Apply a Style to a Cell or Range of Cells

officefileapi-12097-spreadsheet-document-api-examples-formatting-how-to-apply-a-style-to-a-cell-or-range-of-cells.md

latest2.8 KB
Original Source

How to: Apply a Style to a Cell or Range of Cells

  • Sep 19, 2023
  • 2 minutes to read

This example demonstrates how to format a cell, a range of cells, an entire row or an entire column by applying a style.

  1. Access the Style object that specifies the style to be applied to a cell or a range of cells. This style should be added to the Workbook.Styles collection.
  2. Assign the required style object to the CellRange.Style property of the cell, cell range, row or column object.

View Example

csharp
Worksheet worksheet = workbook.Worksheets[0];

// Access the built-in "Good" MS Excel style from the Styles collection of the workbook.
Style styleGood = workbook.Styles[BuiltInStyleId.Good];

// Apply the "Good" style to a range of cells.
worksheet.Range["A1:C4"].Style = styleGood;

// Access a custom style that has been previously created in the loaded document by its name.
Style customStyle = workbook.Styles["Custom Style"];

// Apply the custom style to the cell.
worksheet.Cells["D6"].Style = customStyle;

// Apply the "Good" style to the eighth row.
worksheet.Rows[7].Style = styleGood;

// Apply the custom style to the "H" column.
worksheet.Columns["H"].Style = customStyle;
vb
Dim worksheet As Worksheet = workbook.Worksheets(0)

' Access the built-in "Good" MS Excel style from the Styles collection of the workbook.
Dim styleGood As Style = workbook.Styles(BuiltInStyleId.Good)

' Apply the "Good" style to a range of cells.
worksheet.Range("A1:C4").Style = styleGood

' Access a custom style that has been previously created in the loaded document by its name.
Dim customStyle As Style = workbook.Styles("Custom Style")

' Apply the custom style to the cell.
worksheet.Cells("D6").Style = customStyle

' Apply the "Good" style to the eighth row.
worksheet.Rows(7).Style = styleGood

' Apply the custom style to the "H" column.
worksheet.Columns("H").Style = customStyle

The image below shows how worksheet cells are formatted via styles (the workbook is opened in Microsoft® Excel®).

See Also

How to: Clear Cell Formatting

How to: Create or Modify a Cell Style