- 5 min to read
The proper formatting of worksheet cells improves document appearance and allows end-users to read, find and understand the data that it contains more easily. Cell formatting includes a variety of features such as multiple settings for font, font size, character style (bold, italics, underlined), text alignment, background and foreground colors, etc. This document explains such concepts as Cell Styles Direct Cell Formatting Style Flags and Rich Text Formatting and provides links to examples on how to format cells using the Spreadsheet Document API.
A style is a named set of predefined cell format characteristics (font settings, number format, content alignment, cell borders, fill color, etc.). When applying a style, all format settings are applied to a cell or cell range in a single step.
A workbook keeps all available styles in a StyleCollection collection, which is accessed via the Workbook.Styles property. By default, this collection contains a set of built-in cell styles similar to those found in Microsoft® Excel® (including the Normal style that is applied to all unformatted cells in the workbook by default). Identifiers of all built-in styles are listed by the BuiltInStyleId enumerator.
The image below shows the gallery of style in Microsoft® Excel®.
You can do the following to manage the workbook's collection of cell styles via the Spreadsheet Document API.
- Modify an existing style by changing the properties of the corresponding Style object. Use the Formatting.BeginUpdate - Formatting.EndUpdate method pair to make multiple modifications to a style.
- Create a new custom style by adding a new Style object to the IWorkbook.Styles collection (the StyleCollection.Add method). Note that new styles are created based on the Normal style by default.
- Duplicate an existing style by creating a new style and copying all format settings from an existing style via the Style.CopyFrom method.
For examples, see the How to: Create or Modify a Style document.
Direct Cell Formatting
To change cell appearance, you can not only apply a style, but also set the required formatting characteristics for an individual cell or cell range directly. This is called direct cell formatting. In Microsoft® Excel®, direct cell formatting options are available via the Ribbon interface or in the Format Cells dialog.
To perform direct cell formatting via the Spreadsheet Document API, change the cell or cell range properties that are inherited from the Formatting interface (Formatting.Fill, Formatting.Font, Formatting.Alignment, Formatting.Borders and Formatting.NumberFormat). By default, these properties are set according to the style applied to a cell. Use the following approaches.
- To format an individual cell, access the corresponding Cell object (see How to: Access a Cell in a Worksheet) and modify its formatting properties.
- To format a range of cells, access and modify the Formatting object using the CellRange.BeginUpdateFormatting - CellRange.EndUpdateFormatting method pair.
Thus, a Cell or CellRange object's properties inherited from the Formatting interface provide access to the actual formatting specified for a cell or range of cells (including the characteristics defined by an applied style and direct formatting attributes).
The actual appearance of a cell is a combination of settings specified by the applied style and the direct cell format settings. Each formatting type provides a set of flags (Formatting.Flags). Each flag corresponds to a specific group of format attributes. You can use these flags when formatting a cell, to control whether to use attributes specified in the applied style or attributes specified directly for the cell.
|Alignment||Horizontal and vertical alignment of cell content, indentation and text wrap.||StyleFlags.Alignment|
|Borders||Cell border line styles and colors.||StyleFlags.Borders|
|Fill||Cell background color.||StyleFlags.Fill|
|Font||Cell font settings (name, style, color and size).||StyleFlags.Font|
|Number Format||Cell number format.||StyleFlags.Number|
|Protection||Cell protection options (Locked and Hidden).||StyleFlags.Protection|
Rich Text Formatting
A cell in a worksheet can contain rich formatted text specified by the RichTextString object. Rich text is comprised of one or more text regions (or text runs), each with its own set of font characteristics. An individual text run is defined by the RichTextRun object and stored in the RichTextString.Runs collection.
You can apply rich formatting to cell text as follows:
Use the RichTextString.AddTextRun method to compose cell text from individual text runs.
Use the RichTextString.Characters method overloads to format specific text characters.
Refer to the How to: Apply Rich Formatting to Cell Text document for examples on how to apply different fonts to desired regions of cell text.
The following examples explain how to format worksheet cells via the Spreadsheet Document API.
- How to: Format a Cell or Range of Cells
- How to: Apply Rich Formatting to Cell Text
- How to: Specify Number or Date Format for Cell Content
- How to: Change Cell Font and Background Color
- How to: Configure Cell Font Settings
- How to: Align Cell Content
- How to: Add and Remove Cell Borders
- How to: Clear Cell Formatting
- Conditional Formatting