Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Create or Modify a Cell Style

  • 4 minutes to read

#Create a Custom Style

#Create a New Style

Use the StyleCollection.Add method of the Workbook.Styles collection to create a new style with a given name. The style’s format settings are identical to the Normal built-in style. Use the Style object’s properties to specify format characteristics for the new style.

View Example

// Add a new style under the "My Style" name to the style collection.
Style myStyle = workbook.Styles.Add("My Style");

// Specify the style's format characteristics.
myStyle.BeginUpdate();
try {
    // Set the font color to blue.
    myStyle.Font.Color = Color.Blue;

    // Set the font size to 12.
    myStyle.Font.Size = 12;

    // Center text in cells.
    myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

    // Set the background fill.
    myStyle.Fill.BackgroundColor = Color.LightBlue;
    myStyle.Fill.PatternType = PatternType.LightGray;
    myStyle.Fill.PatternColor = Color.Yellow;
} 
finally {
    myStyle.EndUpdate();
}

#Create a New Style Based on a Built-In Style

  1. Use the StyleCollection.Add method to add a new style to the Workbook.Styles collection.

  2. Call the Style.CopyFrom method for the created style to copy all format settings from the specified Microsoft® Excel® built-in style to the new style.

View Example

// Add a new style under the "My Good Style" name to the style collection.
Style myGoodStyle = workbook.Styles.Add("My Good Style");

// Copy all format settings from the built-in Good style.
myGoodStyle.CopyFrom(BuiltInStyleId.Good);

// Modify the style's format characteristics if needed.
// ...

#Create a New Style Based on Existing Cell Formatting

Use the StyleCollection.Add method overload with the range parameter to create a custom style based on format attributes of an existing cell or cell range.

// Create a style based on format settings of the "B2" cell.
Style customStyle = workbook.Styles.Add("Custom Style", worksheet["B2"]);

// Modify the style's format characteristics if needed.
// ...

#Modify a Style

  1. Use a style‘s index or name to obtain it from the Workbook.Styles collection.

  2. To change the style’s format attributes, modify the Style object’s properties within the Formatting.BeginUpdate - Formatting.EndUpdate method calls.

View Example

// Access a style you want to modify.
Style customStyle = workbook.Styles["Custom Style"];

// Change the style's format characteristics.
customStyle.BeginUpdate();
try {
    customStyle.Fill.BackgroundColor = Color.Gold;
    // ...
} finally {
    customStyle.EndUpdate();
}

You can also use the CellRange.Style property to access and modify a style applied to a specific cell or cell range. Style modifications are propagated to all other cells that use this style.

// Modify the style applied to the "F10" cell.
workbook.Worksheets[0].Cells["F10"].Style.Fill.BackgroundColor = Color.SeaShell;

// Modify the style applied to the "K8:M11" cell range.
workbook.Worksheets[0].Range.Parse("K8:M11").Style.Font.Color = Color.Red;