A cell style is a predefined set of formatting options, such as cell borders, fonts, font sizes and number formats. Using cell styles allows you to apply multiple format options in one step and also offers an easy approach to achieve consistency in cell formatting. The document model has a number of built-in cell styles that you can modify or apply directly. Its API allows you to duplicate an existing style or create a new one according to your preferences.

You can create cell styles that depend on the current document theme. Such styles are updated automatically when you change the selected document theme. You can read more about document themes and dependent cell styles in the Document Themes article.

A cell style is represented by the CellStyle class. The properties of the class can be categorized into five groups: number, alignment, font, border and fill. Following are all properties distributed in their corresponding groups:

Number group

Format

Alignment group

HorizontalAlignment

VerticalAlignment

Indent

IsWrapped

Font group

FontFamily

FontSize

IsBold

IsItalic

Underline

ForeColor

Border group

LeftBorder

TopBorder

RightBorder

BottomBorder

DiagonalUpBorder

DiagonalDownBorder

Fill group

Fill

In addition to the properties above, the CellStyle class exposes five Boolean properties that indicate whether the groups above will be applied:

IncludeNumber

IncludeAlignment

IncludeFont

IncludeBorder

IncludeFill

When you apply a style to a cell with locally set properties, the end result is an addition of the style properties to the cell's local properties. The end result of such an addition of styles depends on which elements (groups) of the style have been selected as being part of the style. You can select a group to be part of the style by setting the appropriate property to true.

Creating a new style is pretty straight-forward. All you have to do is invoke the Add() method of workbook's Styles collection. The method returns an object of type CellStyle, which you can manipulate. Note that setting several properties raises multiple UI updates. To avoid the additional overhead, you can enclose the code blocks that set the properties with the BeginUpdate() and EndUpdate() methods.

You can also remove a style from the Styles collection. It is as easy as removing an object from a list, you simply invoke the Remove() method, which returns a Boolean value. If such style exist in the collection, the method will return true.