How to Add Table to DOCX

Table is a grid of cells arranged in rows and columns. It's used to organize and present information in a structured format. Each intersection of a row and column is a cell, which can contain text, numbers, or other types of data. Tables are commonly used to arrange data neatly, create schedules, or format information in a visually organized manner.

C# NuGet Library for

Install with NuGet

Install-Package IronWord

Add Table Example

A table is a significant component of a Word document. For simplicity, let's start with the smallest element of a table, which is a cell. From the cell, we can add text, images, shapes, paragraphs, or even an entire table. Next, we add the cell to a table row and the table row to the table. Finally, we can instantiate a new Word document from this table or use the AddTable method to add a table to an existing Word document.

:path=/static-assets/word/content-code-examples/how-to/add-table-add-table.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Create table cell
TableCell cell = new TableCell();

TextRun textRun = new TextRun();
textRun.Text = "Sample text";

// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.Color.Black);
borderStyle.BorderValue = BorderValues.Thick;
borderStyle.BorderSize = 5;

// Configure table border
TableBorders tableBorders = new TableBorders()
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

cell.Borders = tableBorders;

// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create table and add row
Table table = new Table();
table.AddRow(row);

// Create new Word document from the table
WordDocument doc = new WordDocument(table);

// Export Word document
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Create table cell
Private cell As New TableCell()

Private textRun As New TextRun()
textRun.Text = "Sample text"

' Add textrun to the cell
cell.AddContent(New Paragraph(textRun))

' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black)
borderStyle.BorderValue = BorderValues.Thick
borderStyle.BorderSize = 5

' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

cell.Borders = tableBorders

' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)

' Create table and add row
Dim table As New Table()
table.AddRow(row)

' Create new Word document from the table
Dim doc As New WordDocument(table)

' Export Word document
doc.SaveAs("document.docx")
VB   C#
Add table

The AddContent method of the TableCell class accepts a ContentElement object, which includes everything from paragraphs, images, and shapes to the table itself. In this case, you can have nested tables, providing a very useful illustration for certain use cases.

Available Border Values

Explore all the available options for border values that can be set using the BorderValues enum:

Border values