Turn worksheet cells into a named Excel table with headers and a table style.
dotnet add package RecursiveAI.OneDocument.SpreadsheetsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); var sheet = book.Sheets[0]; sheet["A1"].SetString("Item"); sheet["A2"].SetString("Pencils"); sheet.Tables.Add("Stock", CellRange.Parse("A1:A2")); book.SaveAs("stock.xlsx");
Writes stock.xlsx with a named table over A1:A2.
Unzip the example, then dotnet run.
The full program
The download creates its own input cells: Item and Quantity in the header, Pencils and 12 in the first data row, then Pens and 6 in an added row. It styles, resizes and renames the table, saves, and reopens the workbook to read its table metadata.
using RecursiveAI.OneDocument.Spreadsheets; using (var book = Workbook.Create()) { var sheet = book.Sheets[0]; sheet["A1"].SetString("Item"); sheet["B1"].SetString("Quantity"); sheet["A2"].SetString("Pencils"); sheet["B2"].SetNumber(12); var table = sheet.Tables.Add("Stock", CellRange.Parse("A1:B2")); table.SetStyle("TableStyleMedium9"); sheet["A3"].SetString("Pens"); sheet["B3"].SetNumber(6); table.Resize(CellRange.Parse("A1:B3")); table.Rename("Inventory"); book.SaveAs("inventory.xlsx"); } using var reopened = Workbook.Open("inventory.xlsx"); var saved = reopened.Sheets[0].Tables.Single(); Console.WriteLine($"{saved.Name}: {saved.Range}"); Console.WriteLine($"Headers: {string.Join(", ", saved.Columns.Select(c => c.Name))}"); Console.WriteLine($"Data: {saved.DataRange}"); Console.WriteLine($"Style: {saved.StyleName}");
Inventory: A1:B3 Headers: Item, Quantity Data: A2:B3 Style: TableStyleMedium9
An invalid or duplicate table name throws ArgumentException; resizing also refuses overlaps with another table or merged cells in the header.
Variations
Normalize duplicate and non-text headers. This independent snippet creates a table in memory:
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); var sheet = book.Sheets[0]; sheet["A1"].SetString("Name"); sheet["B1"].SetNumber(5); sheet["C1"].SetString("Name"); var table = sheet.Tables.Add("People", CellRange.Parse("A1:C3")); Console.WriteLine(string.Join(", ", table.Columns.Select(c => c.Name)));
Name, Column2, Name2Related recipes
The short answer
Populate the worksheet cells, then call sheet.Tables.Add(name, range). Save the workbook with SaveAs. This creates a structured table part, not just a colored range or a Word table. The full example reopens the saved file and reads the table’s columns, data range and style to show what was stored.
OneDocument’s table documentation separates table metadata from the calculation layer: adding a table is not a request to evaluate structured-reference formulas.
How do you create a structured table in Excel?
In C#, create or open a workbook, choose a worksheet, and add the table to its Tables collection. Supply the table’s name and its complete range, including the header row when hasHeaderRow is true, the default. In the snippet at the top, A1 is the header and A2 is the one data row.
The operation creates the table XML part, its worksheet relationship and the sheet’s tablePart element. That distinction matters when the recipient needs an actual Excel table rather than cells that happen to have borders. A table exposes a name and display name, header and totals row counts, columns, a data range and an autofilter range.
Names must be valid defined names, must not resemble a cell reference and must be unique among the workbook’s tables and workbook-scoped names. Uniqueness is case-insensitive: Stock and stock do not identify two different available names. Use a descriptive name such as Inventory, not a coordinate such as A1.
How do you create a table in Excel from existing data?
Open the existing workbook instead of calling Workbook.Create, then select the worksheet and range that already contain the data. Adding the table does not require copying those data cells into a separate structure. It does, however, write header cells so that their text matches the table’s column names.
The full program demonstrates extending an existing table: it writes the new row’s values and explicitly calls Resize to include that row. Writing cells outside the table does not itself show that the table’s stored range changed. After resizing, inspect Range for the full extent and DataRange for the body without the header or totals row.
Rename updates table attributes and SetStyle changes the stored style name. The saved example uses TableStyleMedium9. Removing the table removes its part and relationship but leaves cell values intact. These are separate decisions: remove table structure when the cells should remain, and edit cells when the data should change.
How do you create a table in Excel with a header?
Place text in the first row before calling Add with its default header behavior. Text headers keep their text unless a repeated name needs a numeric suffix. Non-text cells become ColumnN, where N is the column position. The variation shows the exact result for a text header, a numeric header and a duplicate text header.
Plan for normalization when downstream code addresses columns by name: read the resulting Columns collection rather than assuming that every input header survived unchanged. Resize adds or removes columns at the end and rewrites header cells that do not hold their column’s name. It rejects a merged header cell or a range overlapping another table. Choose a clean rectangular range before adding or resizing, then save and reopen if you need to inspect the persisted table rather than only the in-memory object.