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.

Get started with IronWord

Start using IronWord in your project today with a free trial.

First Step:
green arrow pointer


Add Table Example

A table is a significant component of a Word document. Firstly, instantiate the Table class by providing the number of rows and columns. From there, the table's styling, such as background color, shading, border, zebra striping, and width, can be customized. Secondly, each cell of the table can be accessed in a very intuitive way by specifying the row and column of the table in [row, column] format. In each cell, text, images, shapes, paragraphs, or even entire tables can be added. Finally, this table can be added to the Word document.

Tips
All row and column index positions follow zero-based indexing.

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

WordDocument doc = new WordDocument();

// Create table
Table table = new Table(5, 3);

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

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

// Apply styling
table.Zebra = new ZebraColor("FFFFFF", "dddddd");
table.Borders = tableBorders;

// Populate table
table[0, 0] = new TableCell(new Text("Number"));
table[0, 1] = new TableCell(new Text("First Name"));
table[0, 2] = new TableCell(new Text("Last Name"));
for (int i = 1; i < table.Rows.Count; i++)
{
    table[i, 0].AddChild(new Text($"{i}"));
    table[i, 1].AddChild(new Text($"---"));
    table[i, 2].AddChild(new Text($"---"));
}

// Add table
doc.AddTable(table);

doc.Save("document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

Private doc As New WordDocument()

' Create table
Private table As New Table(5, 3)

' Configure border style
Private borderStyle As New BorderStyle()
borderStyle.BorderColor = 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
}

' Apply styling
table.Zebra = New ZebraColor("FFFFFF", "dddddd")
table.Borders = tableBorders

' Populate table
table(0, 0) = New TableCell(New Text("Number"))
table(0, 1) = New TableCell(New Text("First Name"))
table(0, 2) = New TableCell(New Text("Last Name"))
For i As Integer = 1 To table.Rows.Count - 1
	table(i, 0).AddChild(New Text($"{i}"))
	table(i, 1).AddChild(New Text($"---"))
	table(i, 2).AddChild(New Text($"---"))
Next i

' Add table
doc.AddTable(table)

doc.Save("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 Stylings

Borders

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

Border values