Table of Contents

Tables

XlsxDocumentSheet.Tables lists a sheet's tables and makes a range a table, as Excel's Format as Table does. A table is renamed, resized and restyled, given a totals row, converted to a range, and setting its header cell renames the column: every structured reference to it, in every formula of the workbook, follows. Measured against Excel: a table made here is Excel's own part, and Excel opens what DocWright writes without repair.

dotnet add package DocWright.Spreadsheet

Make a table

using DocWright.Spreadsheet;

XlsxTable table = orders.Tables.Add(new XlsxCellRange(1, 1, 5, 4));
Console.WriteLine($"{table}: {string.Join(", ", table.Columns.Select(c => c.Name))}; style {table.Style}");

The columns are named from the header cells as Excel names them: an empty header is Column and a number, a repeated one gets a number after it (Qty, Qty2), from one count the two share. The header cells then hold those names as text. Merged cells inside the range are unmerged, as Excel does. A range without a header row (hasHeaders: false) becomes a table with its header row turned off, its columns Column1 on; no cell moves.

Rename

using DocWright.Spreadsheet;

table.Name = "Orders";
table.Style = "TableStyleLight9";
orders.SetValue("B1", XlsxCellValue.Text("Ordered"));      // renames the column
Console.WriteLine($"F1 = {orders.GetCell("F1").Formula}");

A table's name is unique among the workbook's tables and defined names, without regard to case, and has no space. Renaming a table, or setting one of its header cells, carries every structured reference with it: calculated columns, totals, and formulas on any sheet. Find and Replace and placeholders on a header rename its column too.

Resize

using DocWright.Spreadsheet;

orders.SetValue("E1", XlsxCellValue.Text("Note"));
table.Resize(new XlsxCellRange(1, 1, 6, 5));
Console.WriteLine($"{table}: {string.Join(", ", table.Columns.Select(c => c.Name))}");

try
{
    orders.Tables.Add(new XlsxCellRange(4, 4, 8, 6));
}
catch (InvalidOperationException error)
{
    Console.WriteLine(error.Message);
}

The header row stays where it is and a table keeps at least one row of data; tables never overlap. Download the workbook: orders-table.xlsx.

Totals

using DocWright.Spreadsheet;

table.HasTotalsRow = true;                                   // Excel's Total Row
table.Columns[1].TotalsFunction = XlsxTotalsFunction.Sum;   // SUBTOTAL(109,Orders[Ordered])
Console.WriteLine($"{table}: {orders.GetCell("A7").Value.TextValue}, {orders.GetCell("B7").Formula}");

Turning the totals row on takes the row under the table, with Total in the first column and, in the last, a sum of numbers or a count of anything else, as Excel's Total Row does. A column's function is Excel's SUBTOTAL. Excel moves the cells under a table down to make room; DocWright refuses while a cell there holds something.

Convert to a range

using DocWright.Spreadsheet;

table.Remove();                                              // Excel's Convert to Range
Console.WriteLine($"F1 = {orders.GetCell("F1").Formula}; tables left {orders.Tables.Count}");

Remove() is Excel's Convert to Range: the cells stay, and every structured reference to the table becomes the cells it named, qualified by the sheet (Orders[Ordered] becomes Orders!$B$2:$B$6; [@Qty] in a row becomes a reference to that row). Remove(keepCells: false) clears the cells, and a reference to the table becomes #REF!. A column inserted inside a table becomes a column of it; one deleted leaves it.

The whole run prints:

Table1 A1:D5: Item, Qty, Qty2, Column3; style TableStyleMedium2
F1 = SUM(Orders[Ordered])
Orders A1:E6: Item, Ordered, Qty2, Column3, Note
A table can't overlap another table: Orders covers A1:E6.
Orders A1:E7: Total, SUBTOTAL(109,Orders[Ordered])
F1 = SUM(Orders!$B$2:$B$6); tables left 0