You can do the following to manage the workbook's collection of table styles.
A table style (TableStyle) consists of a collection of table style elements (TableStyle.TableStyleElements). Each table style element (TableStyleElement) specifies formatting for a particular element of a table. The TableStyleElementType enumerator lists the supported table style element types.
Use properties of the TableStyleElement object to customize borders (TableStyleElement.Borders), fill (TableStyleElement.Fill) and font (TableStyleElement.Font) for the corresponding table element.
If you wish to create a style providing striped row or column formatting for a table, customize the FirstRowStripe, SecondRowStripe, FirstColumnStripe or SecondColumnStripe table style elements and set their TableStyleElement.StripeSize property, which specifies the banding rule.
Thus, to modify a table style, follow the steps below.
-
Access the table style to be changed. To do this, get the corresponding TableStyle object from the IWorkbook.TableStyles collection by the table style name.
Note
Built-in table styles cannot be modified. To check whether a table style is built-in or custom, use the TableStyle.BuiltIn property.
- Call the TableStyle.BeginUpdate method.
-
Access the table style element to be modified from the TableStyle.TableStyleElements collection by the corresponding TableStyleElementType enumeration member. Use the TableStyleElement properties to specify the required formatting for the element. If you need to remove existing formatting from the element, use its TableStyleElement.Clear method.
Repeat this step for all table style elements you wish to modify.
- Call the TableStyle.EndUpdate method.
C# |
using DevExpress.Spreadsheet;
TableStyle tableStyle = workbook.TableStyles["tableStyleName"];
tableStyle.BeginUpdate();
try {
TableStyleElement wholeTable = tableStyle.TableStyleElements[TableStyleElementType.WholeTable];
TableStyleElement tableHeader = tableStyle.TableStyleElements[TableStyleElementType.HeaderRow];
TableStyleElement firstColumn = tableStyle.TableStyleElements[TableStyleElementType.FirstColumn];
}
finally {
tableStyle.EndUpdate();
}
|
VB |
Imports DevExpress.Spreadsheet
Dim tableStyle As TableStyle = workbook.TableStyles("testTableStyle")
tableStyle.BeginUpdate()
Try
Dim wholeTable As TableStyleElement = tableStyle.TableStyleElements(TableStyleElementType.WholeTable)
Dim tableHeader As TableStyleElement = tableStyle.TableStyleElements(TableStyleElementType.HeaderRow)
Dim firstColumn As TableStyleElement = tableStyle.TableStyleElements(TableStyleElementType.FirstColumn)
Finally
tableStyle.EndUpdate()
End Try
|
After a table style is changed, the style modifications are automatically applied to all tables that use this style. For details on how a table style is applied to a table and table elements, review the How to: Apply a Table Style to a Table document.
Create a New Table Style
This example demonstrates how to create a custom style to format tables.
-
Add a new table style to the IWorkbook.TableStyles collection by calling the TableStyleCollection.Add method with the table style name passed as a parameter. This method returns the TableStyle object that represents the newly created table style. This object's TableStyle.Name property is set to the specified name. Other settings are identical to the None table style (the default built-in table style that specifies no formatting for a table).
Note
Note that table styles have unique names in the collection. To ensure that there is no table style under the specified name in the collection, use the TableStyleCollection.Contains method.
- Modify elements of the created table style (TableStyle.TableStyleElements) within the TableStyle.BeginUpdate and TableStyle.EndUpdate paired methods.
TableActions.cs |
Table table = worksheet.Tables[0];
String styleName = "testTableStyle";
if (workbook.TableStyles.Contains(styleName))
{
table.Style = workbook.TableStyles[styleName];
}
else
{
TableStyle customTableStyle = workbook.TableStyles.Add("testTableStyle");
customTableStyle.BeginUpdate();
try
{
customTableStyle.TableStyleElements[TableStyleElementType.WholeTable].Font.Color = Color.FromArgb(107, 107, 107);
TableStyleElement headerRowStyle = customTableStyle.TableStyleElements[TableStyleElementType.HeaderRow];
headerRowStyle.Fill.BackgroundColor = Color.FromArgb(64, 66, 166);
headerRowStyle.Font.Color = Color.White;
headerRowStyle.Font.Bold = true;
TableStyleElement totalRowStyle = customTableStyle.TableStyleElements[TableStyleElementType.TotalRow];
totalRowStyle.Fill.BackgroundColor = Color.FromArgb(115, 193, 211);
totalRowStyle.Font.Color = Color.White;
totalRowStyle.Font.Bold = true;
TableStyleElement secondRowStripeStyle = customTableStyle.TableStyleElements[TableStyleElementType.SecondRowStripe];
secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234);
secondRowStripeStyle.StripeSize = 1;
}
finally
{
customTableStyle.EndUpdate();
}
table.Style = customTableStyle;
}
|
TableActions.vb |
' Access a table.
Dim table As Table = worksheet.Tables(0)
Dim styleName As String = "testTableStyle"
' If the style under the specified name already exists in the collection,
If workbook.TableStyles.Contains(styleName) Then
' apply this style to the table.
table.Style = workbook.TableStyles(styleName)
Else
' Add a new table style under the "testTableStyle" name to the TableStyles collection.
Dim customTableStyle_Renamed As TableStyle = workbook.TableStyles.Add("testTableStyle")
' Modify the required formatting characteristics of the table style.
' Specify the format for different table elements.
customTableStyle_Renamed.BeginUpdate()
Try
customTableStyle_Renamed.TableStyleElements(TableStyleElementType.WholeTable).Font.Color = Color.FromArgb(107, 107, 107)
' Specify formatting characteristics for the table header row.
Dim headerRowStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.HeaderRow)
headerRowStyle.Fill.BackgroundColor = Color.FromArgb(64, 66, 166)
headerRowStyle.Font.Color = Color.White
headerRowStyle.Font.Bold = True
' Specify formatting characteristics for the table total row.
Dim totalRowStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.TotalRow)
totalRowStyle.Fill.BackgroundColor = Color.FromArgb(115, 193, 211)
totalRowStyle.Font.Color = Color.White
totalRowStyle.Font.Bold = True
' Specify banded row formatting for the table.
Dim secondRowStripeStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.SecondRowStripe)
secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234)
secondRowStripeStyle.StripeSize = 1
Finally
customTableStyle_Renamed.EndUpdate()
End Try
' Apply the created custom style to the table.
table.Style = customTableStyle_Renamed
End If
|
The following image shows the table formatted with the custom table style created by the code above.

Duplicate an Existing Table Style
You can create new custom table styles based on existing table styles (for example, based on built-in table styles). To do this, use the TableStyle.Duplicate method. This method creates a copy of the specified style and returns the TableStyle object representing the newly created style. You can use properties of this object to change the style format settings.
For example, the code snippet below demonstrates how to duplicate an existing style and modify the new style a bit by removing the table header row formatting copied from the source table style.
Form1.cs |
TableStyle DuplicateAndModifyTableStyle(IWorkbook workbook, string sourceStyleName) {
TableStyle sourceTableStyle = workbook.TableStyles[sourceStyleName];
TableStyle newTableStyle = sourceTableStyle.Duplicate();
newTableStyle.TableStyleElements[TableStyleElementType.HeaderRow].Clear();
return newTableStyle;
}
|
Form1.vb |
Private Function DuplicateAndModifyTableStyle(ByVal workbook As IWorkbook, ByVal sourceStyleName As String) As TableStyle
' Get the table style to be duplicated.
Dim sourceTableStyle As TableStyle = workbook.TableStyles(sourceStyleName)
' Duplicate the table style.
Dim newTableStyle As TableStyle = sourceTableStyle.Duplicate()
' Modify the required formatting characteristics of the created table style.
' For example, remove exisitng formatting from the header row element.
newTableStyle.TableStyleElements(TableStyleElementType.HeaderRow).Clear()
Return newTableStyle
End Function
|
The image below shows the TableStyleMedium21 built-in table style and its modified copy.

To remove a table style from the IWorkbook.TableStyles collection, use the TableStyleCollection.Remove method. After a table style is deleted, all tables to which this style is applied will be formatted with the default style (TableStyleCollection.DefaultStyle).
Note
Built-in table styles cannot be removed. To check whether a table style is built-in or custom, use the TableStyle.BuiltIn property.