Work with Table Styles
- 11 minutes to read
A table style defines the appearance of a table. The Spreadsheet Document API ships with 60 predefined table styles. You can apply a predefined style to a table or create a custom table style.
A table style can specify formatting for the entire table, header and total rows, first and last columns, and banded rows and columns.
Apply a Table Style
Use the Table.StyleName property to apply a table style to a table.
The following code snippet applies the “TableStyleLight1” style to an existing table:
Worksheet worksheet = workbook.Worksheets[0];
Table table = worksheet.Tables[0];
if(table != null)
table.StyleName = PredefinedTableStyleNames.Light1;
Set Table.StyleName to an empty string or “None” to remove the table style from the table.
The following code snippet removes the style from a table:
Apply a Predefined Table Style
The Spreadsheet Document API ships with 60 predefined table styles. Predefined styles are available in the workbook’s Workbook.TableStyles collection.
The PredefinedTableStyleNames class contains constants with predefined table style names. Use these constants to apply predefined styles and to access them in the Workbook.TableStyles collection.
The following code snippet applies the TableStyleMedium2 style to a table and retrieves this style from the collection:
table.StyleName = PredefinedTableStyleNames.Medium2;
TableStyle tableStyle = workbook.TableStyles[PredefinedTableStyleNames.Medium2];
Use the TableStyle.IsPredefined property to identify predefined styles. The following code snippet checks whether a table style is predefined:
Note
Predefined table styles are read-only. You cannot modify their formatting, replace them, or remove them from the Workbook.TableStyles collection.
Create a Custom Table Style
Create a custom table style to customize a predefined style or define a new table style. Add the custom style to the Workbook.TableStyles collection.
Custom table style names must meet the following requirements:
- The name cannot be empty or “None”.
- The name must be unique.
Important
Style names are case-insensitive and must be unique within a workbook. For example, Test and test are the same style name. The API throws an InvalidOperationException if you add a style with a name that already exists.
The following code snippet creates a custom copy of a predefined table style and applies it to a table:
Worksheet worksheet = workbook.Worksheets[0];
TableStyle customTableStyle = workbook.TableStyles[PredefinedTableStyleNames.Medium2].Clone("CustomStyle");
// Customize the custom table style.
workbook.TableStyles.Add(customTableStyle);
Table table = worksheet.Tables[0];
if(table != null )
table.StyleName = "CustomStyle";
Use the Workbook.TableStyles.Add method to create a custom table style and add it to the Workbook.TableStyles collection.
Customize Table Style Options
A table style contains separate elements for different parts of a table. Use the following Table class properties to apply formatting for table elements.
| Table property | Style elements | Description |
|---|---|---|
| HasHeaderRow | HeaderRow, FirstHeaderCell, LastHeaderCell | Specifies whether the table displays a header row. |
| HasTotalRow | TotalRow, FirstTotalCell, LastTotalCell | Specifies whether the table displays a total row. |
| HasFirstColumn | FirstColumn | Specifies whether the table applies first-column formatting. |
| HasLastColumn | LastColumn | Specifies whether the table applies last-column formatting. |
| HasBandedRows | FirstRowStripe, SecondRowStripe | Specifies whether the table applies alternating row formatting. |
| HasBandedColumns | FirstColumnStripe, SecondColumnStripe | Specifies whether the table applies alternating column formatting. |
The following code snippet enables header, total, first-column, last-column, and banded-row formatting:
table.HasHeaderRow = true;
table.HasTotalRow = true;
table.HasFirstColumn = true;
table.HasLastColumn = true;
table.HasBandedRows = true;
Configure Table Style Elements
A TableStyle contains separate style elements for different parts of a table.
| Property | Applies to |
|---|---|
| WholeTable | The entire table. |
| HeaderRow | The header row. |
| TotalRow | The total row. |
| FirstColumn | The first table column. |
| LastColumn | The last table column. |
| FirstRowStripe | The first group in alternating row stripes. |
| SecondRowStripe | The second group in alternating row stripes. |
| FirstColumnStripe | The first group in alternating column stripes. |
| SecondColumnStripe | The second group in alternating column stripes. |
| FirstHeaderCell | The first cell in the header row. |
| LastHeaderCell | The last cell in the header row. |
| FirstTotalCell | The first cell in the total row. |
| LastTotalCell | The last cell in the total row. |
Each TableStyleElement includes the Format property. Use this property to specify font, fill, border, alignment, protection, and number formatting.
The following code snippet configures the font for the entire table and the header row:
Worksheet worksheet = workbook.Worksheets[0];
TableStyle customTableStyle = workbook.TableStyles.Add("CustomTableStyle");
customTableStyle.WholeTable.Format = new DifferentialFormat {
Font = new DifferentialFontFormat()
.WithName("Arial")
.WithSize(14)
};
customTableStyle.HeaderRow.Format = new DifferentialFormat {
Font = new DifferentialFontFormat()
.WithBold(true)
.WithItalic(true)
};
Table table = worksheet.Tables[0];
table.StyleName = "CustomTableStyle";
Format the Table
Use the DifferentialFormat class to specify the formatting that a table style element applies.
The following table lists formatting categories:
| Property | Type | Description |
|---|---|---|
| Font | DifferentialFontFormat | Specifies font properties. |
| Fill | DifferentialFillFormat | Specifies the cell fill. |
| Borders | DifferentialBorderFormat | Specifies table and cell borders. |
| Alignment | DifferentialAlignmentFormat | Specifies cell alignment. |
| Protection | DifferentialProtectionFormat | Specifies cell protection settings. |
| NumberFormat | NumberFormat | Specifies the number format. |
The following code snippet creates a custom table style with double outer borders, thin inner borders, and centered, wrapped header text:
Worksheet worksheet = workbook.Worksheets[0];
Table table = worksheet.Tables[0];
TableStyle tableStyle = workbook.TableStyles.Add("CustomTableStyle");
BorderLine tableBorder = new BorderLine(BorderLineStyle.Double);
BorderLine cellBorder = new BorderLine(BorderLineStyle.Thin);
tableStyle.WholeTable.Format = new DifferentialFormat {
Borders = new DifferentialBorderFormat()
.WithInsideHorizontal(cellBorder)
.WithInsideVertical(cellBorder)
.WithBottom(tableBorder)
.WithTop(tableBorder)
.WithLeft(tableBorder)
.WithRight(tableBorder)
};
tableStyle.HeaderRow.Format = new DifferentialFormat {
Alignment = new DifferentialAlignmentFormat()
.WithHorizontal(SpreadsheetHorizontalAlignment.Center)
.WithVertical(SpreadsheetVerticalAlignment.Center)
.WithWrapText(true),
};
table.StyleName = tableStyle.Name;
Format Header and Total Rows
Use TableStyle.HeaderRow and TableStyle.TotalRow properties to specify formatting for header and total rows.
Use TableStyle.FirstTotalCell and TableStyle.LastTotalCell properties to specify formatting for the first and last cells in the total row.
The following code snippet applies different background fills to the header row and first/last columns:
Worksheet worksheet = workbook.Worksheets[0];
DifferentialFormat rowFormat = new DifferentialFormat {
Fill = new DifferentialSolidFillFormat(CellColor.FromColor(Color.AliceBlue))
};
DifferentialFormat columnFormat = new DifferentialFormat {
Fill = new DifferentialSolidFillFormat(CellColor.FromColor(Color.DarkBlue))
};
TableStyle customTableStyle = workbook.TableStyles.Add("CustomTableStyle");
customTableStyle.HeaderRow.Format = rowFormat;
customTableStyle.FirstHeaderCell.Format = columnFormat;
customTableStyle.LastHeaderCell.Format = columnFormat;
customTableStyle.FirstColumn.Format = columnFormat;
customTableStyle.LastColumn.Format = columnFormat;
Table table = worksheet.Tables[0];
table.HasHeaderRow = true;
table.HasFirstColumn = true;
table.HasLastColumn = true;
table.StyleName = "CustomTableStyle";
Format Banded Rows and Columns
Define Alternating Row Formatting
Use TableStyle.FirstRowStripe and TableStyle.SecondRowStripe properties to define alternating row formatting.
Use TableStyle.FirstColumnStripe and TableStyle.SecondColumnStripe properties to define alternating column formatting.
The following code snippet applies different font formatting to alternating row groups:
Worksheet worksheet = workbook.Worksheets[0];
TableStyle customTableStyle = workbook.TableStyles.Add("CustomTableStyle");
customTableStyle.FirstRowStripe.Format = new DifferentialFormat {
Font = new DifferentialFontFormat()
.WithBold(true)
};
customTableStyle.SecondRowStripe.Format = new DifferentialFormat {
Font = new DifferentialFontFormat()
.WithItalic(true)
};
Table table = worksheet.Tables[0];
table.HasBandedRows = true;
table.StyleName = "CustomTableStyle";
Specify Stripe Size
The TableStyleStripeElement.StripeSize property specifies the number of consecutive rows or columns in each stripe. The value can range from 1 to 9.
The following code snippet applies each row style to two consecutive rows:
The following code snippet applies each column style to two consecutive columns:
tableStyle.FirstColumnStripe.StripeSize = 2;
tableStyle.SecondColumnStripe.StripeSize = 2;
Note
Cell protection settings take effect when worksheet protection is enabled.
Reuse Cell Formatting in Table Styles
Use the following From methods to convert regular cell formatting objects to differential formatting objects:
| Method | Description |
|---|---|
| DifferentialFormat.From(CellFormat) | Converts cell formatting to differential formatting. |
| DifferentialFontFormat.From(FontFormat) | Converts font formatting to differential font formatting. |
| DifferentialFillFormat.From(FillFormat) | Converts fill formatting to differential fill formatting. The method supports solid, pattern, and gradient fills. |
| DifferentialBorderFormat.From(BorderFormat) | Converts border formatting to differential border formatting. |
| DifferentialAlignmentFormat.From(AlignmentFormat) | Converts alignment formatting to differential alignment formatting. |
| DifferentialProtectionFormat.From(ProtectionFormat) | Converts protection formatting to differential protection formatting. |
Note
The NumberFormat property uses the same value type in regular and differential formats. The conversion copies the number format directly.
The following code snippet creates a table style based on the formatting of a template cell:
Worksheet worksheet = workbook.Worksheets[0];
Cell templateCell = worksheet.Cells["A7"];
TableStyle tableStyle = new TableStyle("Report");
tableStyle.WholeTable.Format = DifferentialFormat.From(templateCell.Format);
workbook.TableStyles.Add(tableStyle);
Table table = worksheet.Tables[0];
table.StyleName = tableStyle.Name;
You can also convert individual formatting components.
The following code snippet converts font formatting and assigns it to the header row:
using DevExpress.Docs.Spreadsheet;
using System.IO;
Directory.CreateDirectory("output");
using (FileStream inputStream = File.OpenRead(Path.Combine("input", "Document.xlsx")))
using (FileStream outputStream = File.Create(Path.Combine("output", "Document_Updated.xlsx")))
using (Workbook workbook = new Workbook(inputStream)) {
Worksheet worksheet = workbook.Worksheets[0];
FontFormat font = new FontFormat("Consolas", 10)
.WithBold(true)
.WithColor(CellColor.FromRgb(0, 0, 255));
TableStyle tableStyle = new TableStyle("CustomTableStyle");
tableStyle.HeaderRow.Format = new DifferentialFormat {
Font = DifferentialFontFormat.From(font)
};
workbook.TableStyles.Add(tableStyle);
Table table = worksheet.Tables[0];
table.StyleName = tableStyle.Name;
workbook.Save(outputStream);
}
Remove Custom Table Styles
Call the TableStyleCollection.Remove(item) or TableStyleCollection.RemoveAt(index) method to remove a custom table style. To remove all custom table styles, call the TableStyleCollection.Reset() method.
Tables that use a removed custom style keep the style name and lose the style formatting. If you add a table style with this name again, the style applies to these tables.
The following code snippet removes custom table styles from a workbook and applies a predefined style to a table that uses a removed style:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = "Product";
worksheet.Cells["B1"].Value = "Price";
worksheet.Cells["A2"].Value = "Chai";
worksheet.Cells["B2"].Value = 18;
Table table = new Table(0, 0, 2, 2);
worksheet.Tables.Add(table, true);
TableStyle style = workbook.TableStyles.Add("Price List");
table.StyleName = style.Name;
// Remove custom table styles.
workbook.TableStyles.Reset();
// The table keeps the name of the removed style.
if (workbook.TableStyles[table.StyleName] == null)
table.StyleName = PredefinedTableStyleNames.Medium2;
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);