Skip to main content
A newer version of this page is available. .

How to: Create or Modify a Style

  • 4 minutes to read

Create Your Own Custom Style

  • Create a New Style

    1. Add a new style to the IWorkbook.Styles collection by calling the StyleCollection.Add method with the style name passed as a parameter. This method returns the Style object corresponding to the created style. This object’s Style.Name property is set to the specified name. Other settings are identical to the “Normal” built-in style.
    2. Use properties of the newly created Style object to set the required format characteristics.
    // Add a new style under the "My Style" name to the Styles collection of the workbook.
    Style myStyle = workbook.Styles.Add("My Style");
    
    // Specify formatting characteristics for the style.
    myStyle.BeginUpdate();
    try {
        // Set the font color to Blue.
        myStyle.Font.Color = Color.Blue;
    
        // Set the font size to 12.
        myStyle.Font.Size = 12;
    
        // Set the horizontal alignment to Center.
        myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
    
        // Set the background.
        myStyle.Fill.BackgroundColor = Color.LightBlue;
    }
    finally {
        myStyle.EndUpdate();
    }
    

    End-users can select an existing custom style from the collection of available styles, and use it to format cells.

    SpreadsheetControl_NewStyle

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

    1. Add a new style to the IWorkbook.Styles collection by calling the StyleCollection.Add method with the style name passed as a parameter. This method returns the Style object corresponding to the created style. This object’s Style.Name property is set to the specified name. Other settings are identical to the Normal built-in style.
    2. To copy all format settings from a Microsoft® Excel® built-in style to the newly created style, use the Style.CopyFrom method with the built-in style id (a member of the BuiltInStyleId enumeration) passed as a parameter.
    3. Use properties of the newly created Style object to change the required format settings of the style.
    // Add a new style under the "My Good Style" name to the Styles collection.
    Style myGoodStyle = workbook.Styles.Add("My Good Style");
    
    // Copy all format settings from the built-in Good style.
    myGoodStyle.CopyFrom(BuiltInStyleId.Good);
    

    The following image shows that a copy of the “Good” built-in style has been added to the collection of styles available to end-users, after executing the code above.

    SpreadsheetControl_CopyStyle

Modify an Existing Style

  1. Access a style to be modified. To do this, get the corresponding Style object from the IWorkbook.Styles collection by the style name or index.
  2. Use the style’s Formatting.BeginUpdate and Formatting.EndUpdate paired methods to modify the required format attributes of the style.
// Change the required formatting characteristics of the style.
myGoodStyle.BeginUpdate();
try {
    myGoodStyle.Fill.BackgroundColor = Color.LightYellow;
    // ...
}
finally {
    myGoodStyle.EndUpdate();
}

You can also modify a style by accessing it directly from the cell or cell range to which this style is applied. To do this, use the Range.Style property. Style modifications will automatically be applied to all 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;