Back to Devexpress

Worksheet.Tables Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dc2e1704.md

latest9.6 KB
Original Source

Worksheet.Tables Property

Provides access to the collection of tables contained in the worksheet.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
TableCollection Tables { get; }
vb
ReadOnly Property Tables As TableCollection

Property Value

TypeDescription
TableCollection

A TableCollection object specifying the worksheet’s collection of tables.

|

Example

This example demonstrates how to create a table from a range of cells. The sample range includes a list of products and invoice information on each product: price, quantity and discount.

The resulting table will provide an additional column to calculate the amount per product, and an additional row to show the total amount. Follow the steps below:

  1. Create a Table

  2. Format the Table

  3. Table Columns

  4. Table Header Row

  5. Calculated Column

  6. Table Total Row

  7. Total Row Function

  8. Table Ranges and Data Ranges

  9. The image below shows the results.

View Example

vb
' Insert a table in the worksheet.
Dim table As Table = worksheet.Tables.Add(worksheet("B2:F5"), True)

' Format the table by applying a built-in table style.
table.Style = workbook.TableStyles(BuiltInTableStyleId.TableStyleMedium27)

' Access table columns and name them.
Dim productColumn As TableColumn = table.Columns(0)
productColumn.Name = "Product"
Dim priceColumn As TableColumn = table.Columns(1)
priceColumn.Name = "Price"
Dim quantityColumn As TableColumn = table.Columns(2)
quantityColumn.Name = "Quantity"
Dim discountColumn As TableColumn = table.Columns(3)
discountColumn.Name = "Discount"
Dim amountColumn As TableColumn = table.Columns(4)
amountColumn.Name = "Amount"

' Set the formula to calculate the amount per product 
' and display results in the "Amount" column.
amountColumn.Formula = "=[Price]*[Quantity]*(1-[Discount])"

' Display the total row in the table.
table.ShowTotals = True

' Set the label and function to display the sum of the "Amount" column.
discountColumn.TotalRowLabel = "Total:"
amountColumn.TotalRowFunction = TotalRowFunction.Sum

' Specify the number format for each column.
priceColumn.DataRange.NumberFormat = "$#,##0.00"
discountColumn.DataRange.NumberFormat = "0.0%"
amountColumn.Range.NumberFormat = "$#,##0.00;$#,##0.00;"""";@"

' Specify horizontal alignment for header and total rows of the table.
table.HeaderRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
table.TotalRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

' Specify horizontal alignment to display data in all columns except the first one.
For i As Integer = 1 To table.Columns.Count - 1
    table.Columns(i).DataRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
Next i

' Set the width of table columns.
table.Range.ColumnWidthInCharacters = 10
csharp
// Insert a table in the worksheet.
Table table = worksheet.Tables.Add(worksheet["B2:F5"], true);

// Format the table by applying a built-in table style.
table.Style = workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium27];

// Access table columns and name them.
TableColumn productColumn = table.Columns[0];
productColumn.Name = "Product";
TableColumn priceColumn = table.Columns[1];
priceColumn.Name = "Price";
TableColumn quantityColumn = table.Columns[2];
quantityColumn.Name = "Quantity";
TableColumn discountColumn = table.Columns[3];
discountColumn.Name = "Discount";
TableColumn amountColumn = table.Columns[4]; 
amountColumn.Name = "Amount";

// Set the formula to calculate the amount per product 
// and display results in the "Amount" column.
amountColumn.Formula = "=[Price]*[Quantity]*(1-[Discount])";

// Display the total row in the table.
table.ShowTotals = true;

// Set the label and function to display the sum of the "Amount" column.
discountColumn.TotalRowLabel = "Total:";
amountColumn.TotalRowFunction = TotalRowFunction.Sum;

// Specify the number format for each column.
priceColumn.DataRange.NumberFormat = "$#,##0.00";
discountColumn.DataRange.NumberFormat = "0.0%";
amountColumn.Range.NumberFormat = "$#,##0.00;$#,##0.00;\"\";@";

// Specify horizontal alignment for header and total rows of the table.
table.HeaderRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
table.TotalRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

// Specify horizontal alignment to display data in all columns except the first one.
for (int i = 1; i < table.Columns.Count; i++)
{
    table.Columns[i].DataRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
}

// Set the width of table columns.
table.Range.ColumnWidthInCharacters = 10;

The following code snippets (auto-collected from DevExpress Examples) contain references to the Tables 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.

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/TableActions.cs#L88

csharp
// Access a table.
Table table = worksheet.Tables[0];

winforms-spreadsheet-use-cell-range-as-data-source/CS/RangeDataSource/Form1.cs#L48

csharp
Worksheet sheet = spreadsheetControl1.ActiveWorksheet;
Table sheetDataTable = sheet.Tables[0];
if (sheetDataTable.ShowHeaders)

winforms-spreadsheet-use-custom-cell-editors/CS/DevAVInvoicing/Form1.cs#L142

csharp
string customerId = invoice["B11"].Value.TextValue;
Table storesTable = customerStores.Tables[0];
storesTable.AutoFilter.Clear();

wpf-spreadsheet-bind-grid-control-to-worksheet-data/CS/WpfSpreadsheet_DataBinding/MainWindow.xaml.cs#L26

csharp
// Access the table on the worksheet.
Table expensesTable = worksheet.Tables[0];
// Specify the data source settings.

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/TableActions.vb#L82

vb
' Access a table.
Dim table As Table = worksheet.Tables(0)

winforms-spreadsheet-use-cell-range-as-data-source/VB/RangeDataSource/Form1.vb#L53

vb
Dim sheet As Worksheet = spreadsheetControl1.ActiveWorksheet
Dim sheetDataTable As Table = sheet.Tables(0)
If sheetDataTable.ShowHeaders Then

winforms-spreadsheet-use-custom-cell-editors/VB/DevAVInvoicing/Form1.vb#L130

vb
Dim customerId As String = invoice("B11").Value.TextValue
Dim storesTable As Table = customerStores.Tables(0)
storesTable.AutoFilter.Clear()

wpf-spreadsheet-bind-grid-control-to-worksheet-data/VB/WpfSpreadsheet_DataBinding/MainWindow.xaml.vb#L24

vb
' Access the table on the worksheet.
Dim expensesTable As Table = worksheet.Tables(0)
' Specify the data source settings.

See Also

Add

Remove(Table)

RemoveAt(Int32)

Clear()

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace