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.

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;
using System.Drawing;

// Create a new Word document instance
WordDocument doc = new WordDocument();

// Define a table with 5 rows and 3 columns
Table table = new Table(5, 3);

// Configure the border style for the table
BorderStyle borderStyle = new BorderStyle
{
    BorderColor = Color.Black,         // Use black color for border
    BorderValue = BorderValues.Thick,  // Set border thickness to 'Thick'
    BorderSize = 5                     // Set border size to 5
};

// Apply border styles to all sides of the table
TableBorders tableBorders = new TableBorders
{
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle
};

// Apply zebra styling (alternating row colors) and borders to the table
table.Zebra = new ZebraColor("FFFFFF", "DDDDDD"); // White and light gray for zebra stripes
table.Borders = tableBorders;

// Set headers for the 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"));

// Fill the table with mock data
for (int i = 1; i < table.NumberOfRows; i++)  // Make sure to use the correct property for the number of rows
{
    table[i, 0] = new TableCell(new TextContent($"{i}"));
    table[i, 1] = new TableCell(new TextContent("---")); // Placeholder for First Name
    table[i, 2] = new TableCell(new TextContent("---")); // Placeholder for Last Name
}

// Add the configured table to the document
doc.AddTable(table);

// Save the document to a file
doc.Save("document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports System.Drawing

' Create a new Word document instance
Private doc As New WordDocument()

' Define a table with 5 rows and 3 columns
Private table As New Table(5, 3)

' Configure the border style for the table
Private borderStyle As New BorderStyle With {
	.BorderColor = Color.Black,
	.BorderValue = BorderValues.Thick,
	.BorderSize = 5
}

' Apply border styles to all sides of the table
Private tableBorders As New TableBorders With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

' Apply zebra styling (alternating row colors) and borders to the table
table.Zebra = New ZebraColor("FFFFFF", "DDDDDD") ' White and light gray for zebra stripes
table.Borders = tableBorders

' Set headers for the 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"))

' Fill the table with mock data
For i As Integer = 1 To table.NumberOfRows - 1 ' Make sure to use the correct property for the number of rows
	table(i, 0) = New TableCell(New TextContent($"{i}"))
	table(i, 1) = New TableCell(New TextContent("---")) ' Placeholder for First Name
	table(i, 2) = New TableCell(New TextContent("---")) ' Placeholder for Last Name
Next i

' Add the configured table to the document
doc.AddTable(table)

' Save the document to a file
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

What is a table in the context of a Word document?

A table is a grid of cells arranged in rows and columns used to organize and present information in a structured format in a Word document.

How can I add a table to a Word document using IronWord?

To add a table to a Word document using IronWord, you need to download a C# library, populate cells with content, create a table by adding rows, initialize a Word document with the table, and then export the document.

What are some styling options available for tables in IronWord?

IronWord allows customization of table styling such as background color, shading, borders, zebra striping, and width.

What method is used to add content to a table cell in IronWord?

The AddChild method of the TableCell class is used to add content to a table cell in IronWord.

Can tables be nested within other tables in IronWord?

Yes, tables can be nested within other tables in IronWord, which can be useful for certain use cases.

What is the index position format used for accessing table cells in IronWord?

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

What is the BorderValues enum in IronWord?

The BorderValues enum in IronWord provides options for setting the border styles of tables in a Word document.

How do you save a Word document after adding a table in IronWord?

After adding a table to a Word document in 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 entire 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, such as var table = new Table(3, 3) for a 3x3 table.

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.