Recursive AI

Add a table to a Word document in C#

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

The table uses equal-width columns and Word’s Table Grid style.

Terminal
dotnet add package RecursiveAI.OneDocument.Documents

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Documents;
 
using var doc = Document.Create();
doc.Body.AddTable([
    ["Region", "Sales"],
    ["East", "100"],
    ["West", "200"]
]);
doc.SaveAs("table.docx");
Console.WriteLine("Wrote table.docx");

dotnet run prints:

Text
Wrote table.docx

Rows may have different lengths; shorter rows get empty cells. A table cannot have more than 63 columns.

Unzip the example, then dotnet run.

Variations

Create an empty grid and fill its cells.

C#
using RecursiveAI.OneDocument.Documents;
 
using var doc = Document.Create();
var table = doc.Body.AddTable(2, 2);
table.SetCellText(0, 0, "Item");
table.SetCellText(0, 1, "Quantity");
table.SetCellText(1, 0, "Pens");
table.SetCellText(1, 1, "12");
table[0, 0].Paragraphs().First()
    .Runs().First().Bold = true;
doc.SaveAs("inventory.docx");

Writes inventory.docx with a bold “Item” header; it prints nothing.

Append a table to an existing DOCX.

Use table.docx created by the snippet at the top. Its Region/Sales table stays in place; this adds an Item/Quantity table after it.

C#
using RecursiveAI.OneDocument.Documents;
 
using var doc = Document.Open("table.docx");
doc.Body.AddTable([
    ["Item", "Quantity"],
    ["Pens", "12"]
]);
doc.SaveAs("appended.docx");

Writes appended.docx; it prints nothing.

The short answer

Call doc.Body.AddTable with rows of strings, then save the document as DOCX. The snippet at the top creates a table with Region and Sales columns and two data rows. It uses Word’s Table Grid style; no template or desktop Word process is needed.

How do I insert a table into a Word document?

Open an existing document with Document.Open or create one with Document.Create, then add the table through its body. AddTable always adds the table at the end of the document. If you need surrounding prose, add an introductory paragraph first and a following paragraph after the table.

The string rows describe cell contents, not markup. In the example, “100” and “200” are text in Word cells; they are not spreadsheet values and do not create a formula. Choose the document model for the output you need: use Word tables for a report’s rows and columns, and a spreadsheet when you need spreadsheet calculations.

You can pass jagged arrays, lists or a two-dimensional nullable string array. Short rows are padded with empty cells, which helps when optional fields are absent. The longest row determines how many columns are needed. The authoring documentation specifies the 63-column maximum and the default layout: equal-width columns spanning the page’s text width. When section dimensions are absent, that width defaults to 6.5 inches.

What does a C# example for adding a Word table look like?

The snippet at the top is a complete program. Its array contains all the input: a header, East with sales of 100, and West with sales of 200. The downloadable project pins the Documents package to 1.0.0. Run it and open table.docx; the program creates its own data and needs no input document.

For data that arrives one cell at a time, use the variation’s AddTable(2, 2) overload. It allocates an empty two-row, two-column table. SetCellText fills each cell by zero-based row and column index. The first header is therefore at (0, 0), and the first item is at (1, 0).

A table cell contains paragraphs and runs, so formatting text inside a cell follows the same model as other Word text. The variation locates the first paragraph and first run in the Item header cell and makes that run bold. It does not make every header cell bold automatically. Apply the same change to the other header cells if that is your intended appearance.

The table indexer reaches a cell by its grid position. For a merged region, any position covered by that region returns its cell, as documented in the authoring guide. Do not assume every visual grid position necessarily represents a separate text container when working with an existing document.

If you need to preserve existing content, open that file and save to a different output path. Creating a document starts from a new body; it does not load or append to an existing file just because the output filename already exists.

All onedocument recipes