如何使用 IronWord 在 C# 中向 DOCX 添加表格
使用 IronWord,可以在 C# 中创建具有指定行数和列数的 Table 对象,以编程方式向 Word 文档添加表格。保存为 DOCX 文件之前,可以设置边框和颜色,并为单元格填充内容。
本示例演示如何在 IronWord 中创建表格。指定表格大小,应用样式并添加内容,然后将表格插入文档并保存。几分钟内即可生成包含已设置样式的表格的 DOCX 文件。
-
1使用 IronWord 安装 NuGet Package Manager
-
2复制并运行这段代码。
var table = new IronWord.Models.Table(3,4); var doc = new IronWord.WordDocument(); doc.AddTable(table); doc.SaveAs("QuickTable.docx");Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx")Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronWord
基本流程(5 步)
- 下载用于在DOCX中添加表格的C#库
- 填充单元格,并将单元格组成行
- 通过添加行创建表格
- 使用该表格初始化一个新的 Word 文档,并将表格追加到文档中
- 导出最终的Word文档
如何在 Word 文档中添加表格?
表格是 Word 文档的基本组成部分。首先,指定行数和列数来实例化 Table 类,并设置表格样式,包括背景色、底纹、边框、交替行底色和宽度。接着,使用 [row, column] 索引访问各个单元格,添加文本、图像、形状、段落,甚至是表格。最后,将表格添加到 Word 文档中。
IronWord 中的表格可灵活组织 Word 文档中的结构化数据。无论是创建发票、报告还是数据摘要,都可以通过 Table 类控制表格内容和呈现方式。从 0 开始的索引便于以编程方式遍历单元格,丰富的样式选项可使表格呈现专业的外观。
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");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");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim 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.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 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
' Add table
doc.AddTable(table)
doc.Save("document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim 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.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 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
' Add table
doc.AddTable(table)
doc.Save("document.docx")
TableCell 类的 AddChild 方法接受实现 IWordDocumentObject 的内容对象,例如段落、图像、形状和表格,因此可以在复杂场景中创建嵌套表格。
IronWord 提供多种方式管理单元格内容。可以通过构造函数创建带有初始内容的 TableCell,也可以用 AddChild 方法逐项添加内容。一个单元格可以组合多种内容类型,例如先添加标题段落,再插入图像和用于列出详细规格的嵌套表格。
以下示例演示如何向单元格添加多种类型的内容:
// 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// 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' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.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") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $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
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.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") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $49.99"))
' Cells can also contain lists, images, and more
' This demonstrates the versatility of table cells in IronWord可以为表格设置哪些样式?
IronWord 提供丰富的表格样式选项,可用于创建美观、专业的文档。除了设置边框和颜色,还可以调整单元格内边距和对齐方式,并通过交替行底色应用条件格式。样式属性采用常见的名称,枚举值也易于辨识。
有哪些可用的边框样式?
BorderValues 枚举列出了所有可用的边框样式:

BorderValues 枚举提供多种边框样式,从简单的单线到波浪线、点线等复杂样式,可用于不同的设计场景。商务文档可使用双线或三线边框,创意文档可选用波浪线或 DashDot 样式。将 BorderSize 与 BorderValue 搭配使用,可精确控制线宽,单位为 1/8 磅。
下面是一个显示不同边框配置的实用示例:
// 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");// 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");' Example: Applying different borders to table sections
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.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 With {
.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
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.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 With {
.TopBorder = headerBorder,
.BottomBorder = headerBorder,
.LeftBorder = bodyBorder,
.RightBorder = bodyBorder,
.InsideHorizontalBorder = bodyBorder,
.InsideVerticalBorder = bodyBorder
}
' Zebra striping for better row distinction
styledTable.Zebra = New ZebraColor("F5F5F5", "FFFFFF")表格宽度和对齐属性可进一步控制布局。可以指定固定宽度或百分比,设置表格在文档中的对齐方式,并控制表格与周围内容的布局关系。还可以分别设置单元格的背景色、文本对齐方式和内边距,精细调整表格外观。
这些样式选项可满足不同的文档设计需求,从简单的数据网格到具有多层视觉层次的复杂财务报表。
常见问题
如何使用C#在DOCX文件中创建表格?
您可以通过使用IronWord实例化带有所需行数和列数的Table类,在DOCX文件中创建表格。设置样式和内容后,您可以将表格添加到WordDocument中并将其保存为DOCX文件。
在IronWord中有哪些可用的表格样式选项?
IronWord为表格提供了广泛的样式选项,包括边框、颜色、单元格填充和对齐。您可以应用诸如斑马线等条件格式,并使用BorderValues枚举中的各种边框样式来增强表格的美观。
我可以使用IronWord向单个表格单元格添加复杂内容吗?
是的,IronWord允许您使用AddChild方法向单个表格单元中添加复杂内容。单元格可以包含嵌套表格、段落、图像和各种文本元素,支持多样和详细的内容管理。
如何在使用IronWord创建的表格中使用斑马条纹?
要在表格中应用斑马线,请配置IronWord中表格样式的ZebraColor属性,指定交替的行颜色以提高可读性和视觉吸引力。
使用IronWord将表格添加到Word文档的步骤是什么?
要使用IronWord将表格添加到Word文档中,您需要下载IronWord库,创建和样式化您的表格,将其插入到WordDocument中,然后保存文档。
您如何控制IronWord中表格的宽度和对齐方式?
在IronWord中,通过设置表格的相关属性来控制表格的宽度和对齐方式。这允许您在文档中根据需要指定精确的尺寸或百分比并对齐表格。
在IronWord中有哪些方法可用于填充表格数据?
在IronWord中,您可以通过零基索引访问单元格,并使用诸如AddChild之类的方法来编程地将TextContent、图像或其他元素插入到每个单元中,从而填充表格。
IronWord如何支持表格的边框样式?
IronWord支持通过允许您使用BorderStyle类自定义边框颜色、值和大小来实现边框样式。您可以为表格的标题和正文部分创建独特的视觉层次结构。
我可以在IronWord中将表格嵌套在单元格中吗?
是的,IronWord支持通过AddChild方法在单元格内嵌套表格。此功能有助于构建复杂文档的复杂表格结构。
使用IronWord在Word文档中创建表格有哪些好处?
使用IronWord进行表格创建提供了一种灵活和可编程的文档结构方法,允许全面的内容管理、详细的样式设置以及高效的自动化来生成专业的DOCX文件。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。