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.
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.
-
1Install IronWord with NuGet Package Manager
-
2Copy 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");C# -
3Deploy to test on your live environment
Start using IronWord in your project today with a free trial
Minimal Workflow (5 steps)
- Download a C# library for adding tables to DOCX
- Populate cells with content and assemble cells into rows
- Create a table by adding rows to it
- Initialize a new Word document with the table and append the table
- Export the final Word document
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 a professional appearance.
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.Style.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");

The AddChild method of the TableCell class accepts content objects - such as paragraphs, images, shapes, and tables - that implement IWordDocumentObject. 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();
TextContent headerText = new TextContent("Product Details") { IsBold = true, FontSize = 14 };
header.AddText(headerText);
complexCell.AddChild(header);
// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001") { IsBold = true });
complexCell.AddChild(new TextContent("\nPrice: $49.99"));
// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord
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:

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.Style.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");
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 can I create a table in a DOCX file using C#?
You can create a table in a DOCX file using IronWord by instantiating the `Table` class with the desired number of rows and columns. After setting up styling and content, you can add the table to a `WordDocument` and save it as a DOCX file.
What are the styling options available for tables in IronWord?
IronWord offers extensive styling options for tables, including borders, colors, cell padding, and alignment. You can apply conditional formatting like zebra striping and use various border styles from the `BorderValues` enum to enhance table aesthetics.
Can I add complex content to individual table cells using IronWord?
Yes, IronWord allows you to add complex content to individual table cells using the `AddChild` method. Cells can contain nested tables, paragraphs, images, and various text elements, enabling diverse and detailed content management.
How do I use zebra striping in tables created with IronWord?
To apply zebra striping in tables, configure the `ZebraColor` property of the table's style in IronWord, specifying alternating row colors for improved readability and visual appeal.
What are the steps to add a table to a Word document using IronWord?
To add a table to a Word document using IronWord, you need to download the IronWord library, create and style your table, insert it into a `WordDocument`, and then save the document.
How do you control the width and alignment of tables in IronWord?
In IronWord, you control the table's width and alignment by setting the respective properties on the table. This allows you to specify exact dimensions or percentages and align the table within a document as needed.
What methods are available to populate a table with data in IronWord?
In IronWord, you can populate a table by accessing cells through zero-based indexing and using methods like `AddChild` to insert `TextContent`, images, or other elements into each cell programmatically.
How does IronWord support border styling for tables?
IronWord supports border styling by allowing you to customize border color, value, and size using the `BorderStyle` class. You can create distinct visual hierarchies within your tables for both header and body sections.
Can I nest tables within cells using IronWord?
Yes, IronWord supports nesting tables within cells by using the `AddChild` method. This feature assists in constructing intricate table structures for complex documents.
What are the benefits of using IronWord for table creation in Word documents?
Using IronWord for table creation provides a flexible and programmable approach to document structure, allowing comprehensive content management, detailed styling, and efficient automation for generating professional DOCX files.

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.