How to Add Table to DOCX

A 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.

TipsAll 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 TextContent("Number"));
table[0, 1] = new TableCell(new TextContent("First Name"));
table[0, 2] = new TableCell(new TextContent("Last Name"));
for (int i = 1; i < table.Rows.Count; i++)
{
    table[i, 0].AddChild(new TextContent($"{i}"));
    table[i, 1].AddChild(new TextContent($"---"));
    table[i, 2].AddChild(new TextContent($"---"));
}

// 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 TextContent("Number"))
table(0, 1) = New TableCell(New TextContent("First Name"))
table(0, 2) = New TableCell(New TextContent("Last Name"))
For i As Integer = 1 To table.Rows.Count - 1
	table(i, 0).AddChild(New TextContent($"{i}"))
	table(i, 1).AddChild(New TextContent($"---"))
	table(i, 2).AddChild(New TextContent($"---"))
Next i

' Add table
doc.AddTable(table)

doc.Save("document.docx")
$vbLabelText   $csharpLabel
Add table

The AddChild 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

Frequently Asked Questions

How can I add a table to a Word document using C#?

You can add a table to a Word document using IronWord by downloading the C# library, populating cells with content, assembling them into rows, and creating a table by adding these rows. Once the table structure is set, initialize it in a new Word document and export the document.

What styling options are available for tables in IronWord?

IronWord allows you to customize tables with various styling options such as background color, shading, borders, zebra striping, and width.

How do I add content to a specific cell in a table using C#?

In IronWord, you can use the AddChild method of the TableCell class to add content such as text, images, shapes, or even entire tables to a specific cell.

Can I nest tables within other tables programmatically using IronWord?

Yes, IronWord allows you to nest tables within other tables, which can be particularly useful for complex document layouts.

What is the indexing format for accessing table cells in IronWord?

IronWord uses zero-based indexing for accessing table cells, specified in a [row, column] format.

How can I set border styles for tables in a Word document using C#?

You can set border styles for tables in IronWord using the BorderValues enum, which provides various options for customizing table borders.

What method is used to save a Word document after adding a table with IronWord?

After adding a table to a Word document using IronWord, you can save the document using the Save method with the desired file name, such as document.Save('ExampleTable.docx').

What types of content can be added to a table cell in IronWord?

In IronWord, you can add various types of content to a table cell, including text, images, shapes, paragraphs, and even nested tables.

How do you initialize a table in IronWord?

To initialize a table in IronWord, instantiate the Table class by providing the number of rows and columns. For instance, var table = new Table(3, 3) creates a 3x3 table.

What is the process to export a Word document with a table using C#?

To export a Word document with a table using IronWord, first create and style the table, initialize it in the Word document, and then use the Save method to export the document as a .docx file.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.