This topic explains how to set colors on various cells format properties.
This topic contains the following sections:
The following table summarizes the main features of the Excel 2007 Color Model. Additional details are available after the following summary table.
The Color palette has been exposed publicly, via the Workbook
's Palette property, so that you can choose which colors to use when finding the closest matching color. The palette has an indexer to get and set the 56 colors in it, as well as a Reset method to reset it back to its preset state.
The color palette is analogous to the color dialog in Microsoft Excel 2007 UI. You can open this color dialog by navigating to Excel Options>Save>Colors.
The derived types, representing the various fills which can be created, are as follows:
You can create all possible fill types using static properties and methods on the CellFill
class. They are as follows:
You can specify a color (the color of Excel cells background, border, etc) using linear and rectangular gradients in cells. When workbooks with these gradients are saved in XLS file format and opened in Microsoft Excel 2007/2010, the gradients will be visible, but when these files are opened in Microsoft Excel 2003, the cell will be filled with the solid color from the first gradient stop.
These are the ways a color can be defined, as follows:
The automatic color (which is the WindowText
system color)
Any user defined RGB color
A theme color
If an RGB or a theme color is used, an optional tint can be applied to lighten or darken the color. This tint cannot be set directly in Microsoft Excel 2007 UI, but various colors in the color palette displayed to the user are actually theme colors with tints applied.
Each workbook has 12 associated theme colors. They are the following:
Light 1
Light 2
Dark 1
Dark 2
Accent1
Accent2
Accent3
Accent4
Accent5
Accent6
Hyperlink
Followed Hyperlink
They have default values when a workbook is created, and they can be customized via Excel UI.
Colors are defined by the WorkbookColorInfo
class, which is a sealed immutable class. The class has a static Automatic property, which returns the automatic color, and there are various constructors which allow you to create a WorkbookColorInfo
instance with a color or a theme value and an optional tint.
The GetResolvedColor
method allows you to determine what color will actually be seen by the user when they open the file in Excel.
If the WorkbookColorInfo
represents a theme color, you must pass in a Workbook instance to the method so it can get the theme color’s RGB value from the workbook.
When saving out in the newer file formats (Microsoft Excel 2007 and later versions), the newer color information is saved directly into the file. When saving out in an older file format, the index to the closest color in the palette will be saved out. In addition, the older formats have future feature records that can be saved out to indicate the newer color information.
When the older formats are opened in Microsoft Excel 2003 and earlier versions, these future features records are ignored. But when the older file formats are opened in Excel 2007 and later, there records are read and the color information from them overwrites the indexed color that was previously loaded from the normal format records.
The following topics provide additional information related to this topic.