How to Add Table to DOCX C# | IronWord

How to Add a Table to DOCX Using C# with IronWord

IronWord enables developers to programmatically add tables to Word documents in C# by creating Table objects with specified rows and columns, styling them with borders and colors, and populating cells with content before saving as DOCX files.

Quickstart: Create and Save a Table with One Call

This example demonstrates how to create a table in IronWord. Construct the table with dimensions, apply styles, add content, insert it into a document, and save. You can generate a DOCX file with a styled table in minutes.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy and run this code snippet.

    var table = new IronWord.Models.Table(3,4);
    var doc = new IronWord.WordDocument();
    doc.AddTable(table);
    doc.SaveAs("QuickTable.docx");
  3. Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    arrow pointer

How Do I Add a Table to My Word Document?

A table is a fundamental component of Word documents. First, instantiate the Table class by providing the number of rows and columns. Configure the table's styling, including background color, shading, border, zebra striping, and width. Second, access each cell using intuitive [row, column] indexing. Add text, images, shapes, paragraphs, or even tables to each cell. Finally, add the table to the Word document.

Tables in IronWord provide a flexible foundation for organizing structured data in Word documents. Whether creating invoices, reports, or data summaries, the Table class offers comprehensive control over content and presentation. The zero-based indexing system simplifies programmatic cell iteration, while rich styling options ensure professional appearance.

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
Word document showing empty 4x3 table with Number, First Name, Last Name columns and alternating row shading

The AddChild method of the TableCell class accepts a ContentElement object, which includes paragraphs, images, shapes, and tables. This enables nested tables for complex use cases.

When working with table cells, IronWord provides multiple approaches for content management. Instantiate a TableCell with initial content using the constructor, or add content incrementally using the AddChild method. This flexibility allows building complex cell structures that combine different content types. For instance, a single cell might contain a header paragraph followed by an image and a nested table for detailed specifications.

Here's an example demonstrating advanced cell population techniques:

// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();

// Add a styled paragraph
Paragraph header = new Paragraph();
header.Add(new TextContent("Product Details").Bold().FontSize = 14);
complexCell.AddChild(header);

// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001").Bold());
complexCell.AddChild(new TextContent("\nPrice: $49.99"));

// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord
// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();

// Add a styled paragraph
Paragraph header = new Paragraph();
header.Add(new TextContent("Product Details").Bold().FontSize = 14);
complexCell.AddChild(header);

// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001").Bold());
complexCell.AddChild(new TextContent("\nPrice: $49.99"));

// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Styling Options Can I Apply to Tables?

IronWord offers extensive styling capabilities for tables, enabling creation of visually appealing and professional documents. Beyond basic borders and colors, control cell padding, alignment, and apply conditional formatting through zebra striping. The styling system combines power with intuitive design, using familiar property names and clear value enumerations.

Which Border Styles Are Available?

Explore all available options for border values using the BorderValues enum:

Word border styles menu showing options like Single, Double, Triple, Dotted, Dashed, Wave, and various thickness combinations

The BorderValues enumeration provides comprehensive options for table aesthetics. From simple single lines to complex patterns like waves and dots, each style serves specific design purposes. Business documents benefit from professional Double or Triple borders, while creative documents utilize Wave or DashDot patterns. The BorderSize property works with BorderValue to provide precise control over line thickness, measured in eighths of a point.

Here's a practical example showing different border configurations:

// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);

// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
    BorderColor = Color.Navy,
    BorderValue = BorderValues.Double,
    BorderSize = 8
};

BorderStyle bodyBorder = new BorderStyle
{
    BorderColor = Color.Gray,
    BorderValue = BorderValues.Dotted,
    BorderSize = 3
};

// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Borders = new TableBorders
{
    TopBorder = headerBorder,
    BottomBorder = headerBorder,
    LeftBorder = bodyBorder,
    RightBorder = bodyBorder,
    InsideHorizontalBorder = bodyBorder,
    InsideVerticalBorder = bodyBorder
};

// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");
// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);

// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
    BorderColor = Color.Navy,
    BorderValue = BorderValues.Double,
    BorderSize = 8
};

BorderStyle bodyBorder = new BorderStyle
{
    BorderColor = Color.Gray,
    BorderValue = BorderValues.Dotted,
    BorderSize = 3
};

// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Borders = new TableBorders
{
    TopBorder = headerBorder,
    BottomBorder = headerBorder,
    LeftBorder = bodyBorder,
    RightBorder = bodyBorder,
    InsideHorizontalBorder = bodyBorder,
    InsideVerticalBorder = bodyBorder
};

// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Table width and alignment properties provide additional layout control. Set tables to specific widths or percentages, align them within the document, and control interaction with surrounding content. Cell-level styling options include individual background colors, text alignment, and padding adjustments, providing granular control over every aspect of table appearance.

These styling options enable creation of tables matching any document's design requirements, from simple data grids to complex financial statements with multiple visual hierarchies.

Frequently Asked Questions

How do I create a table with specific dimensions in a Word document?

With IronWord, you can create a table by instantiating the Table class and specifying the number of rows and columns. For example, use 'var table = new IronWord.Models.Table(3,4);' to create a 3-row by 4-column table. Then add it to a WordDocument object and save as a DOCX file.

Can I style tables with borders and colors programmatically?

Yes, IronWord allows you to configure comprehensive table styling including background colors, shading, borders, zebra striping, and width. You can apply these styles to your Table object before adding it to the Word document.

How do I access and populate specific cells in a table?

IronWord uses zero-based indexing to access table cells. You can access cells using intuitive [row, column] notation, then populate them with various content types including text, images, shapes, paragraphs, or even nested tables.

What types of content can I add to table cells?

Using IronWord's TableCell class, you can add multiple content types through the AddChild method, which accepts ContentElement objects. This includes paragraphs, images, shapes, and even tables for creating nested table structures.

Is it possible to create nested tables within table cells?

Yes, IronWord supports nested tables. Since the AddChild method accepts ContentElement objects including tables, you can add a table inside a table cell to handle complex data organization requirements.

What's the quickest way to generate a DOCX file with a table?

The fastest approach with IronWord is to create a Table object with dimensions, instantiate a WordDocument, add the table using AddTable(), and save with SaveAs(). This entire process can be completed in just 4 lines of code.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 27,587 | Version: 2025.12 just released