如何使用 IronWord 在 DOCX 中使用 C# 添加表格
IronWord使开发人员能够通过创建具有指定行和列的 Table 对象,设置边框和颜色样式,并在保存为 DOCX 文件之前填充单元格内容,从而以编程方式将表格添加到 C# Word 文档中。
快速入门:一次调用即可创建并保存表格
本示例演示了如何在 IronWord 中创建表格。 使用尺寸构建表格,应用样式,添加内容,将其插入文档并保存。 您可以在几分钟内生成带有样式表的 DOCX 文件。
最小工作流程(5 个步骤)
- 下载用于在DOCX中添加表格的C#库
- 用内容填充单元格并将单元格组装成行
- 通过添加行创建表格
- 用表格初始化新Word文档并附加表格
- 导出最终的Word文档
如何在 Word 文档中添加表格?
表格是 Word 文档的基本组成部分。 首先,通过提供行数和列数来实例化 Table 类。 配置表格的样式,包括背景颜色、阴影、边框、斑马线和宽度。 其次,使用直观的索引访问每个单元格。 在每个单元格中添加文本、图像、形状、段落甚至表格。 最后,将表格添加到 Word 文档中。
IronWord 中的表格为在 Word 文档中组织结构化数据提供了灵活的基础。 无论是创建发票、报告还是数据摘要,Table 类都提供了对内容和呈现方式的全面控制。 零基索引系统简化了程序单元的迭代,而丰富的样式选项则确保了专业的外观。
所有行和列索引位置均采用从零开始的索引。
: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")
AddChild 类的方法接受一个 ContentElement 对象,该对象包含段落、图像、形状和表格。 这使得复杂用例的嵌套表格成为可能。
在处理表格单元格时,IronWord 为内容管理提供了多种方法。 使用构造函数实例化一个 TableCell 并添加初始内容,或者使用 AddChild 方法逐步添加内容。 这种灵活性允许构建结合不同内容类型的复杂单元格结构。例如,单个单元格可能包含一个标题段落,然后是一张图片和一个嵌套表格,用于详细说明。
下面是一个演示高级细胞群技术的示例:
// 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
' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As 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(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 属性配合使用,可以精确控制线条粗细,以八分之一磅为单位进行测量。
下面是一个显示不同边框配置的实用示例:
// 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");
Imports System.Drawing
' 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.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")
表宽和对齐属性提供了额外的布局控制。 将表格设置为特定宽度或百分比,在文档中对齐,并控制与周围内容的交互。 单元格级样式选项包括单独的背景颜色、文本对齐方式和填充调整,可对表格外观的各个方面进行细化控制。
通过这些样式选项,可以创建符合任何文档设计要求的表格,从简单的数据网格到具有多个可视化层次结构的复杂财务报表。
常见问题解答
如何在 Word 文档中创建具有特定尺寸的表格?
使用 IronWord,您可以通过实例化 Table 类并指定行数和列数来创建表格。例如,使用 "var table = new IronWord.Models.Table(3,4); "创建一个 3 行 4 列的表格。然后将其添加到 WordDocument 对象中,并保存为 DOCX 文件。
我可以通过编程为表格设置边框和颜色样式吗?
是的,IronWord 允许您配置全面的表格样式,包括背景颜色、阴影、边框、斑马线和宽度。您可以在将表格对象添加到 Word 文档之前将这些样式应用到表格对象中。
如何访问和填充表格中的特定单元格?
IronWord 使用基于零的索引来访问表格单元格。您可以使用直观的[行、列]符号访问单元格,然后在单元格中填充各种内容类型,包括文本、图像、形状、段落甚至嵌套表格。
我可以在表格单元格中添加哪些类型的内容?
使用 IronWord 的 TableCell 类,您可以通过 AddChild 方法添加多种内容类型,该方法接受 ContentElement 对象。这包括段落、图片、形状,甚至是用于创建嵌套表格结构的表格。
是否可以在表格单元格中创建嵌套表格?
是的,IronWord 支持嵌套表格。由于 AddChild 方法可接受包括表格在内的 ContentElement 对象,因此您可以在表格单元格内添加表格,以处理复杂的数据组织要求。
生成带表格的 DOCX 文件的最快方法是什么?
使用 IronWord 的最快方法是创建一个带尺寸的表格对象,实例化一个 WordDocument,使用 AddTable() 添加表格,然后使用 SaveAs() 保存。整个过程只需 4 行代码即可完成。

