The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Create or Modify a Style

  • 4 min to read

Create Your Own Custom Style

  • Create a New Style

    1. Add a new style to the Workbook.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;
        myStyle.Fill.PatternType = PatternType.LightGray;
        myStyle.Fill.PatternColor = Color.Yellow;
    } 
    finally {
        myStyle.EndUpdate();
    }
    
  • Create a New Style Based on a Built-In Style

    1. Add a new style to the Workbook.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 BuiltInStyleId enumeration member) 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);
    
    // Modify the required formatting characteristics if needed.
    // ...
    

Modify an Existing Style

  1. Access a style to be modified. To do this, get the corresponding Style object from the Workbook.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.
// Access the style to be modified.
Style customStyle = workbook.Styles["Custom Style"];

// Change the required formatting characteristics of the style.
customStyle.BeginUpdate();
try {
    customStyle.Fill.BackgroundColor = Color.Gold;
    // ...
} finally {
    customStyle.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 CellRange.Style property. Style modifications will automatically be applied 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;