怎样在 DOCX C# 中添加表格 | IronWord

How to Add Table to DOCX

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Create and Save a Table with One Call

This example shows how easy it is to spin up a table in IronWord—just construct it with size, set your styles, add content, drop it into a document, and save. You’ll have a DOCX 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

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.

提示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 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

常见问题解答

如何使用C#将表格添加到Word文档中?

您可以通过下载C#库、用内容填充单元格、将它们组装成行及通过添加这些行来创建表格来使用IronWord将表格添加到Word文档中。表格结构设置好后,在新Word文档中初始化它并导出文档。

IronWord中可用于表格的样式选项有哪些?

IronWord允许您使用各种样式选项来自定义表格,例如背景颜色、阴影、边框、斑马条纹和宽度。

如何使用C#向表格中特定的单元格添加内容?

在IronWord中,您可以使用TableCell类的AddChild方法向特定单元格添加内容,如文本、图像、形状,甚至整个表格。

我可以使用IronWord以编程方式在其他表格中嵌套表格吗?

是的,IronWord允许您在其他表格中嵌套表格,这对于复杂的文档布局特别有用。

在IronWord中访问表格单元格的索引格式是什么?

IronWord使用基于零的索引来访问表格单元格,格式为[row, column]

如何使用C#设置Word文档中表格的边框样式?

您可以使用IronWord中的BorderValues枚举为表格设置边框样式,该枚举提供了用于自定义表格边框的各种选项。

使用IronWord添加表格后保存Word文档使用的是哪种方法?

在使用IronWord将表格添加到Word文档后,您可以使用Save方法并指定所需的文件名保存文档,例如document.Save('ExampleTable.docx')

在IronWord中可以向表格单元格添加哪些类型的内容?

在IronWord中,您可以向表格单元格添加各种类型的内容,包括文本、图像、形状、段落,甚至嵌套表格。

如何在IronWord中初始化表格?

要在IronWord中初始化表格,通过提供行数和列数实例化Table类。例如,var table = new Table(3, 3)创建一个3x3的表格。

使用C#导出带有表格的Word文档的过程是怎样的?

要使用IronWord导出带有表格的Word文档,首先创建并设置表格样式,在Word文档中初始化它,然后使用Save方法将文档导出为.docx文件。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布