Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Create and Apply Cell Styles

    • 4 minutes to read

    The Workbook.Styles collection stores cell styles. You can apply a style to individual cells and cell ranges.

    The Workbook.Styles collection initially contains 49 predefined styles. You can apply these styles to cells and cell ranges or create custom styles. Use the Style.IsPredefined property to determine whether a style is predefined or custom.

    The PredefinedStyleNames class contains constants with predefined style names. Use these constants to apply predefined styles and to access them in the Workbook.Styles collection.

    A style includes the following formatting components:

    Property Description
    Font Specifies font settings.
    Fill Specifies the cell fill.
    Borders Specifies cell borders.
    Alignment Specifies cell content alignment.
    NumberFormat Specifies the format of cell values.
    Protection Specifies cell protection settings.

    Apply a Style to a Cell

    Use the Cell.StyleName property to apply a style to an individual cell.

    The following code snippet applies the Heading 1 style to cell A1:

    Cell cell = worksheet.Cells["A1"];
    
    cell.StyleName = PredefinedStyleNames.Heading1;
    

    Apply a Style to a Cell Range

    Use the CellRange.StyleName property to apply a style to all cells in a range.

    The following code snippet applies the Heading 1 style to cells A1:D1:

    CellRange range = worksheet["A1:D1"];
    
    range.StyleName = PredefinedStyleNames.Heading1;
    

    Create a Custom Style

    Use the StyleCollection.Add method to create a custom style and add it to the collection. The method accepts a style name and returns the created Style object.

    try {
        Style headerStyle = workbook.Styles.Add("Header");
    
        headerStyle.Font = new FontFormat("Arial", 12)
            .WithBold(true);
    
        headerStyle.Alignment = new AlignmentFormat(
            SpreadsheetHorizontalAlignment.Center,
            SpreadsheetVerticalAlignment.Center);
    }
    catch (InvalidOperationException) {
        // A style named "Header" already exists in the workbook.
    }
    

    Important

    Style names are case-insensitive and must be unique within a workbook. For example, Test and test are the same style name. The API throws an InvalidOperationException if you add a style with a name that already exists.

    Refer to the following help topic for additional information on individual formatting components: Format Cells and Cell Ranges.

    Modify a Cell Style

    Access a style in the workbook’s Styles collection by its name and modify required formatting settings. These changes are applied to all cells and ranges that use the style.

    The following code snippet modifies font settings of the Heading 1 style:

    workbook.Styles[PredefinedStyleNames.Heading1].Font = new FontFormat("Arial", 22)
        .WithBold(true)
        .WithItalic(true)
        .WithColor(CellColor.FromColor(Color.DarkBlue))
        .WithUnderline(UnderlineType.Double);
    

    Remove a Custom Style

    Use the StyleCollection.Remove method to remove a custom style from the workbook. The following code snippet removes the Header custom style:

    workbook.Styles.Remove(headerStyle);
    

    When you remove a style, the formatting of cells and cell ranges that use the style is reset to the Normal predefined cell style.

    Important

    You cannot remove the Normal style. If you pass the Normal style to the StyleCollection.Remove method, the API throws a System.InvalidOperationException.

    Restore Predefined Cell Styles

    Call the StyleCollection.Reset() method to remove custom cell styles from a workbook and restore predefined cell styles of a new workbook.

    The following code snippet adds a custom cell style, modifies the predefined Title style, and restores cell styles of a new workbook:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Sales Report";
    worksheet.Cells["A2"].Value = "Region";
    
    Style title = workbook.Styles[PredefinedStyleNames.Title];
    title.Font = title.Font.Value.WithColor(CellColor.FromRgb(31, 78, 121));
    worksheet.Cells["A1"].StyleName = PredefinedStyleNames.Title;
    
    Style header = workbook.Styles.Add("Header");
    header.Font = new FontFormat("Arial", 12).WithBold(true);
    worksheet.Cells["A2"].StyleName = header.Name;
    
    // Remove custom cell styles and restore predefined cell styles.
    workbook.Styles.Reset();
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Next Steps

    See Also