Create and Manage Tables
- 10 minutes to read
Spreadsheet Document API allows you to convert ranges of cells into tables. Create and format tables, apply table styles, and manage table data.
Create a Table
Create a Table object and specify the starting cell’s row and column indexes and the number of rows and columns in the table. The table contains data from the worksheet cells in the specified range. Call the TableCollection.Add(Table, Boolean) method to add the table to the worksheet. The hasHeaders parameter specifies whether to treat the first row as a header row.
The following code snippet converts cells A1:B4 to the Products table and applies the predefined TableStyleMedium2 style to the table:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = "Product";
worksheet.Cells["B1"].Value = "Price";
worksheet.Cells["A2"].Value = "Chai";
worksheet.Cells["B2"].Value = 18;
worksheet.Cells["A3"].Value = "Chang";
worksheet.Cells["B3"].Value = 19;
worksheet.Cells["A4"].Value = "Tofu";
worksheet.Cells["B4"].Value = 23.25;
// The first row in the cell range contains column headers.
var table = new Table(0, 0, 4, 2, "Products");
worksheet.Tables.Add(table, true);
table.StyleName = PredefinedTableStyleNames.Medium2;
using var stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Specify Table Headers
Use the table’s Table.Columns collection to access each column by its index, and set the column’s TableColumn.Name property. To hide the header row, disable the Table.HasHeaderRow property.
The hasHeaders parameter you pass to the TableCollection.Add(Table, Boolean) method defines how the table generates column headers:
- If
hasHeadersistrue, the table uses values from the worksheet’s header row as column names. If you assign a column’s TableColumn.Name property before you add the table to the worksheet, this name overrides the corresponding worksheet value. - If
hasHeadersisfalse, the table does not use worksheet cell values as headers. Instead, it uses names you assigned to the TableColumn.Name property before you added the table to the worksheet, or default names (Column1, Column2, and so on) for columns without assigned names.
The following code snippet creates a table with an empty header row and specifies column names:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A2"].Value = "Chai";
worksheet.Cells["B2"].Value = 18;
worksheet.Cells["A3"].Value = "Chang";
worksheet.Cells["B3"].Value = 19;
worksheet.Cells["A4"].Value = "Tofu";
worksheet.Cells["B4"].Value = 23.25;
Table table = new Table(0, 0, 4, 2);
worksheet.Tables.Add(table, true);
// Specify column names in the header row.
table.Columns[0].Name = "Product";
table.Columns[1].Name = "Price";
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Create a Calculated Column
Assign a formula to the TableColumn.Formula property to create a calculated column. This formula applies to all cells in the column, excluding the header and total rows. In the formula, you can refer to table columns by their names.
The following code snippet specifies a formula for the last table column:
using DevExpress.Docs.Spreadsheet;
using System.IO;
// Create a new workbook.
Workbook workbook = new Workbook();
// Access the first worksheet in the workbook.
Worksheet worksheet = workbook.Worksheets[0] as Worksheet;
Table table = new Table(1,1,5,5);
worksheet.Tables.Add(table, true);
// Obtain table columns and specify their names.
TableColumn priceColumn = table.Columns[1];
priceColumn.Name = "Price";
TableColumn quantityColumn = table.Columns[2];
quantityColumn.Name = "Quantity";
TableColumn discountColumn = table.Columns[3];
discountColumn.Name = "Discount";
// Specify the formula to calculate the amount for each product
// and display the result in the "Amount" column.
TableColumn amountColumn = table.Columns[4];
amountColumn.Name = "Amount";
amountColumn.Formula = "=[Price]*[Quantity]*(1-[Discount])";
using (FileStream stream = File.Create("Document.xlsx"))
{
workbook.Save(stream, DocumentFormat.Xlsx);
}
Specify Total Row
Use the Table.HasTotalRow property to display the total row.
The table below lists properties you can use to configure the total row:
| Property | Description |
|---|---|
| TableColumn.TotalRowFunction | Gets or sets the function used to calculate the total row value for the column. |
| TableColumn.TotalRowLabel | Gets or sets the label displayed in the total row for the column. |
| TableColumn.TotalRowArrayFormula | Gets or sets the array formula used to calculate the total row value for the column. |
| TableColumn.TotalRowFormula | Gets or sets the custom formula used to calculate the total row value for the column. |
The following code snippet displays a total row for a table and calculates the total amount for the “Amount” column:
using DevExpress.Docs.Spreadsheet;
using System.IO;
// Create a new workbook.
Workbook workbook = new Workbook();
// Access the first worksheet in the workbook.
Worksheet worksheet = workbook.Worksheets[0] as Worksheet;
Table table = new Table(1,1,5,5);
worksheet.Tables.Add(table, true);
TableColumn discountColumn = table.Columns[3];
TableColumn amountColumn = table.Columns[4];
amountColumn.Name = "Amount";
// Display the total row for the table.
table.HasTotalRow = true;
// Use the SUM function to calculate the total value for the "Amount" column.
discountColumn.TotalRowLabel = "Total: ";
amountColumn.TotalRowFunction = TotalRowFunction.Sum;
using (FileStream stream = File.Create("Document.xlsx"))
{
workbook.Save(stream, DocumentFormat.Xlsx);
}
Filter Table Data
Each table has a separate AutoFilter. Use the Table.AutoFilter property to access the table AutoFilter. If a table has a header row, the table AutoFilter is enabled. The table AutoFilter applies to the table range without the total row.
To filter table rows by values in a column, assign one of the following FilterCriteria descendants to the AutoFilterColumn.Criteria property of an item in the TableAutoFilter.Columns collection:
| Class | Description |
|---|---|
| ValueFilterCriteria | Filter criteria that display rows with the specified values or dates. |
| ComparisonFilterCriteria | Filter criteria that compare cell values with one or two specified values. |
| TopBottomFilterCriteria | Filter criteria that display rows with the highest or lowest numeric values in a column. |
| DynamicFilterCriteria | Filter criteria that display rows that match a dynamic condition (for example, values above average or dates in the current month). |
| FillFilterCriteria | Filter criteria that display rows in which the cell in the column has the specified fill. |
| FontColorFilterCriteria | Filter criteria that display rows in which the cell in the column has the specified font color. |
Use the following members to manage the table AutoFilter:
| Member | Description |
|---|---|
| TableAutoFilter.Update | Applies filter criteria to current cell values and updates row visibility. |
| TableAutoFilter.ClearCriteria | Removes filter criteria from all columns and displays rows that the filter hides. |
| TableAutoFilter.Enabled | Gets or sets whether the table AutoFilter is enabled. |
The following code snippet creates a table, displays table rows with amounts greater than 15, modifies a value in a hidden row, and applies the filter criteria again:
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 = "Amount";
worksheet.Cells["A2"].Value = "Apple";
worksheet.Cells["B2"].Value = 10;
worksheet.Cells["A3"].Value = "Pear";
worksheet.Cells["B3"].Value = 20;
worksheet.Cells["A4"].Value = "Plum";
worksheet.Cells["B4"].Value = 30;
Table table = new Table(0, 0, 4, 2, "Sales");
worksheet.Tables.Add(table, true);
// Display rows with amounts greater than 15.
table.AutoFilter.Columns[1].Criteria = new ComparisonFilterCriteria(15, FilterComparisonOperator.GreaterThan);
// Modify a value in a hidden row and apply the filter criteria again.
worksheet.Cells["B2"].Value = 50;
table.AutoFilter.Update();
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Refer to the following help topic for additional information about how to filter cell ranges outside tables: Filter Data in Worksheets.
Manage Table Styles
Refer to the following topic for information on how to create and apply table styles: Work with Table Styles
The following code snippet creates a table and applies the predefined TableStyleMedium9 style to it:
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;
worksheet.Cells["A3"].Value = "Chang";
worksheet.Cells["B3"].Value = 19;
worksheet.Cells["A4"].Value = "Tofu";
worksheet.Cells["B4"].Value = 23.25;
Table table = new Table(0, 0, 4, 2);
worksheet.Tables.Add(table, true);
table.StyleName = PredefinedTableStyleNames.Medium9;
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Remove a Table
Call the TableCollection.Remove(Table, Boolean) method to remove a table from the worksheet. The clearContent parameter specifies whether to delete the table’s data along with the table.
The following code snippet removes a table from a worksheet and clears table cells:
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;
worksheet.Cells["A3"].Value = "Chang";
worksheet.Cells["B3"].Value = 19;
worksheet.Cells["A4"].Value = "Tofu";
worksheet.Cells["B4"].Value = 23.25;
Table table = new Table(0, 0, 4, 2);
worksheet.Tables.Add(table, true);
// Remove the table and clear table cells.
worksheet.Tables.Remove(table, true);
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);