How to: Apply a Table Style

  • 4 min to read

The example below demonstrates how to format a table by using one of the predefined table styles.

To format a table by applying a table style, assign the corresponding TableStyle object to the Table.Style property. Access the required table style object from the Workbook.TableStyles collection by the table style name. Built-in table styles can also be obtained by their ids (the BuiltInTableStyleId enumeration members).

A table style consists of the collection of table style elements (TableStyle.TableStyleElements). Each table style element (TableStyleElement) specifies the formatting for a particular element of a table. The TableStyleElementType enumerator lists supported table style element types. The Table object provides the following properties to optionally specify table elements to be formatted as defined by the corresponding elements of the applied table style.

This example demonstrates how to access a table style by its name and apply it to an existing table. The Table.ShowHeaders and Table.ShowTotals properties are set to true to show the header and total row of the table. The Table.ShowTableStyleRowStripes and Table.ShowTableStyleColumnStripes properties are used to apply the striped column formatting to the table.

The Table.ShowTableStyleFirstColumn property is also set to true to apply specific formatting to the first column of the table.

Worksheet worksheet = workbook.Worksheets["FormatTable"];
workbook.Worksheets.ActiveWorksheet = worksheet;


// Access a table.
Table table = worksheet.Tables[0];

// Access the workbook's collection of table styles.
TableStyleCollection tableStyles = workbook.TableStyles;

// Access the built-in table style from the collection by its name.
TableStyle tableStyle = tableStyles[BuiltInTableStyleId.TableStyleMedium16];

// Apply the table style to the existing table.
table.Style = tableStyle;

// Show header and total rows and format them as specified by the applied table style.
table.ShowHeaders = true;
table.ShowTotals = true;

// Apply banded column formatting to the table.
table.ShowTableStyleRowStripes = false;
table.ShowTableStyleColumnStripes = true;

// Apply special formatting to the first column of the table. 
table.ShowTableStyleFirstColumn = true;
worksheet.Visible = true;

The image below shows the table appearance when the TableStyleMedium16 style is applied (the workbook is opened in Microsoft® Excel®).

SpreadsheetDocServer_BuiltInTableStyle

See Also