officefileapi-devexpress-dot-spreadsheet-dot-iworkbook-ee17f2e3.md
Provides access to the collection of styles to format tables and pivot tables in the workbook.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
TableStyleCollection TableStyles { get; }
ReadOnly Property TableStyles As TableStyleCollection
| Type | Description |
|---|---|
| TableStyleCollection |
A TableStyleCollection object containing table and pivot table styles.
|
The TableStyles property returns the collection of styles that you can apply to tables or pivot tables to change their appearance. An individual style is specified by the TableStyle object. To apply a style to a table, assign the required table style object to the Table.Style property. To apply a style to a pivot table, assign the required style object to the PivotTable.Style property. For details on how to format tables and pivot tables using styles, see the How to: Apply a Table Style to a Table and How to: Apply a Predefined Style to a Pivot Table examples.
By default, a workbook’s collection of styles contains Microsoft® Excel® built-in table and pivot table styles that can be obtained by their ids (members of the BuiltInTableStyleId and BuiltInPivotStyleId enumerations). Built-in styles cannot be deleted or modified. However, you can create your own custom styles, as well as duplicate, modify or delete them.
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).
Modify elements of the created table style (TableStyle.TableStyleElements) within the TableStyle.BeginUpdate and TableStyle.EndUpdate paired methods.
' 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
// Access a table.
Table table = worksheet.Tables[0];
String styleName = "testTableStyle";
// If the style under the specified name already exists in the collection,
if (workbook.TableStyles.Contains(styleName))
{
// 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.
TableStyle customTableStyle = workbook.TableStyles.Add("testTableStyle");
// Modify the required formatting characteristics of the table style.
// Specify the format for different table elements.
customTableStyle.BeginUpdate();
try
{
customTableStyle.TableStyleElements[TableStyleElementType.WholeTable].Font.Color = Color.FromArgb(107, 107, 107);
// Specify formatting characteristics for the table header row.
TableStyleElement headerRowStyle = customTableStyle.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.
TableStyleElement totalRowStyle = customTableStyle.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.
TableStyleElement secondRowStripeStyle = customTableStyle.TableStyleElements[TableStyleElementType.SecondRowStripe];
secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234);
secondRowStripeStyle.StripeSize = 1;
}
finally
{
customTableStyle.EndUpdate();
}
// Apply the created custom style to the table.
table.Style = customTableStyle;
}
The following code snippets (auto-collected from DevExpress Examples) contain references to the TableStyles property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
WorksheetTableDataBinding sheetDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles[BuiltInTableStyleId.TableStyleMedium14];
spreadsheet-document-api-data-binding/CS/SpreadsheetApiDataBinding/Program.cs#L75
WorksheetTableDataBinding sheetDataBinding = worksheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
sheetDataBinding.Table.Style = worksheet.Workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium14];
// Set the pivot table style.
pivotTable.Style = workbook.TableStyles[BuiltInPivotStyleId.PivotStyleDark7];
#endregion #SetStyle
// Set the pivot table style.
pivotTable.Style = workbook.TableStyles[BuiltInPivotStyleId.PivotStyleDark7];
#endregion #SetStyle
// Set the pivot table style.
pivotTable.Style = workbook.TableStyles[BuiltInPivotStyleId.PivotStyleDark7];
#endregion #SetStyle
Dim sheetDataBinding As WorksheetTableDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions)
sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles(BuiltInTableStyleId.TableStyleMedium14)
spreadsheet-document-api-data-binding/VB/SpreadsheetApiDataBinding/Program.vb#L67
Dim sheetDataBinding As WorksheetTableDataBinding = worksheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions)
sheetDataBinding.Table.Style = worksheet.Workbook.TableStyles(BuiltInTableStyleId.TableStyleMedium14)
' Set the pivot table style.
pivotTable.Style = workbook.TableStyles(BuiltInPivotStyleId.PivotStyleDark7)
' #End Region ' #SetStyle
' Set the pivot table style.
pivotTable.Style = workbook.TableStyles(BuiltInPivotStyleId.PivotStyleDark7)
' #End Region ' #SetStyle
' Set the pivot table style.
pivotTable.Style = workbook.TableStyles(BuiltInPivotStyleId.PivotStyleDark7)
#End Region ' #SetStyle
See Also