How to: Apply a Table Style
- 4 minutes 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.
- Table.ShowHeaders - shows the header row in the table and formats it as specified by the TableStyleElementType.HeaderRow element of the applied table style.
- Table.ShowTotals - shows the total row in the table and formats it as specified by the TableStyleElementType.TotalRow element of the applied table style.
- Table.ShowTableStyleRowStripes - applies striped row formatting to a table as specified by the TableStyleElementType.FirstRowStripe and TableStyleElementType.SecondRowStripe elements of the applied table style.
- Table.ShowTableStyleColumnStripes - applies striped column formatting to a table as specified by the TableStyleElementType.FirstColumnStripe and TableStyleElementType.SecondColumnStripe elements of the applied table style.
- Table.ShowTableStyleFirstColumn - formats the first column of the table as specified by the TableStyleElementType.FirstColumn, TableStyleElementType.FirstHeaderCell and TableStyleElementType.FirstTotalCell elements of the applied table style.
- Table.ShowTableStyleLastColumn - formats the last column of the table as specified by the TableStyleElementType.LastColumn, TableStyleElementType.LastHeaderCell and TableStyleElementType.LastTotalCell 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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/spreadsheet-document-server-api-part-2-t217615
Dim worksheet As Worksheet = workbook.Worksheets("FormatTable")
workbook.Worksheets.ActiveWorksheet = worksheet
' Access a table.
Dim table As Table = worksheet.Tables(0)
' Access the workbook's collection of table styles.
Dim tableStyles As TableStyleCollection = workbook.TableStyles
' Access the built-in table style from the collection by its name.
Dim tableStyle As 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®).