Back to Devexpress

Use the Excel Export API to Change Cell Background Color

officefileapi-114132-excel-export-library-formatting-how-to-change-cell-background-color.md

latest5.6 KB
Original Source

Use the Excel Export API to Change Cell Background Color

  • Sep 19, 2023
  • 3 minutes to read

This example demonstrates how to fill a cell background. To do this, use the static methods of the XlFill object.

  • Solid Fill

  • Pattern Fill

If you use the XlFill.PatternFill method overload that specifies only a pattern style, the cell background color will be empty and the pattern color will be set to automatic.

To apply fill settings to a cell, pass the specified XlFill object to the IXlCell.ApplyFormatting method as a parameter, or assign it to the IXlCell.Formatting property.

Tip

To share background settings with multiple cells in a row at once, use the IXlRow.BlankCells and IXlRow.BulkCells methods.

To specify fill settings for the entire row or column, use the IXlRow.ApplyFormatting and IXlColumn.ApplyFormatting methods, or IXlRow.Formatting and IXlColumn.Formatting properties, respectively.

View Example

csharp
// Create a new worksheet.
using(IXlSheet sheet = document.CreateSheet())
{

    using(IXlRow row = sheet.CreateRow()) {
        using(IXlCell cell = row.CreateCell()) {
            // Fill the cell background using the predefined color.
            cell.ApplyFormatting(XlFill.SolidFill(Color.Beige));
        }
        using(IXlCell cell = row.CreateCell()) {
            // Fill the cell background using the custom RGB color.
            cell.ApplyFormatting(XlFill.SolidFill(Color.FromArgb(0xff, 0x99, 0x66)));
        }
        using(IXlCell cell = row.CreateCell()) {
            // Fill the cell background using the theme color.
            cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent3, 0.4)));
        }
    }

    using(IXlRow row = sheet.CreateRow()) {
        using(IXlCell cell = row.CreateCell()) {
            // Specify the cell background pattern using predefined colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.DarkDown, Color.Red, Color.White));
        }
        using(IXlCell cell = row.CreateCell()) {
            // Specify the cell background pattern using custom RGB colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.DarkTrellis, Color.FromArgb(0xff, 0xff, 0x66), Color.FromArgb(0x66, 0x99, 0xff)));
        }
        using(IXlCell cell = row.CreateCell()) {
            // Specify the cell background pattern using theme colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.LightHorizontal, XlColor.FromTheme(XlThemeColor.Accent1, 0.2), XlColor.FromTheme(XlThemeColor.Light2, 0.0)));
        }
    }
}
vb
' Create a new worksheet.
Using sheet As IXlSheet = document.CreateSheet()

    Using row As IXlRow = sheet.CreateRow()
        Using cell As IXlCell = row.CreateCell()
            ' Fill the cell background using the predefined color.
            cell.ApplyFormatting(XlFill.SolidFill(Color.Beige))
        End Using
        Using cell As IXlCell = row.CreateCell()
            ' Fill the cell background using the custom RGB color.
            cell.ApplyFormatting(XlFill.SolidFill(Color.FromArgb(&Hff, &H99, &H66)))
        End Using
        Using cell As IXlCell = row.CreateCell()
            ' Fill the cell background using the theme color.
            cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent3, 0.4)))
        End Using
    End Using

    Using row As IXlRow = sheet.CreateRow()
        Using cell As IXlCell = row.CreateCell()
            ' Specify the cell background pattern using predefined colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.DarkDown, Color.Red, Color.White))
        End Using
        Using cell As IXlCell = row.CreateCell()
            ' Specify the cell background pattern using custom RGB colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.DarkTrellis, Color.FromArgb(&Hff, &Hff, &H66), Color.FromArgb(&H66, &H99, &Hff)))
        End Using
        Using cell As IXlCell = row.CreateCell()
            ' Specify the cell background pattern using theme colors.
            cell.ApplyFormatting(XlFill.PatternFill(XlPatternType.LightHorizontal, XlColor.FromTheme(XlThemeColor.Accent1, 0.2), XlColor.FromTheme(XlThemeColor.Light2, 0.0)))
        End Using
    End Using
End Using

The image below illustrates the result (the workbook is opened in Microsoft® Excel®).

See Also

Use the Excel Export API to Format a Cell