如何使用 IronWord 在 DOCX 中使用 C# 添加表格
IronWord使开发人员可以通过创建带有指定行和列的Table对象,以编程方式在C#中向Word文档中添加表格,使用边框和颜色为其设置样式,并在保存为DOCX文件之前填充单元格内容。
本示例演示了如何在 IronWord 中创建表格。 使用尺寸构建表格,应用样式,添加内容,将其插入文档并保存。 您可以在几分钟内生成带有样式表的 DOCX 文件。
-
1Install IronWord with 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类都提供了对内容和呈现的全面控制。 基于零的索引系统简化了程序化的单元格迭代,而丰富的样式选项确保了专业的外观。
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")
ContentElement对象,该对象包括段落、图片、形状和表格。 这使得复杂用例的嵌套表格成为可能。
在处理表格单元格时,IronWord 为内容管理提供了多种方法。 使用构造函数初始化含有初始内容的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 模式。 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")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 文档中创建具有特定尺寸的表格?
using IronWord,您可以通过实例化 Table 类并指定行数和列数来创建表格。例如,使用 "var table = new IronWord.Models.Table(3,4); "创建一个 3 行 4 列的表格。然后将其添加到 WordDocument 对象中,并保存为 DOCX 文件。
我可以通过编程为表格设置边框和颜色样式吗?
是的,IronWord 允许您配置全面的表格样式,包括背景颜色、阴影、边框、斑马线和宽度。您可以在将表格对象添加到 Word 文档之前将这些样式应用到表格对象中。
如何访问和填充表格中的特定单元格?
IronWord 使用基于零的索引来访问表格单元格。您可以使用直观的[行、列]符号访问单元格,然后在单元格中填充各种内容类型,包括文本、图像、形状、段落甚至嵌套表格。
我可以在表格单元格中添加哪些类型的内容?
using IronWord 的 TableCell 类,您可以通过 AddChild 方法添加多种内容类型,该方法接受 ContentElement 对象。这包括段落、图片、形状,甚至是用于创建嵌套表格结构的表格。
是否可以在表格单元格中创建嵌套表格?
是的,IronWord 支持嵌套表格。由于 AddChild 方法可接受包括表格在内的 ContentElement 对象,因此您可以在表格单元格内添加表格,以处理复杂的数据组织要求。
生成带表格的 DOCX 文件的最快方法是什么?
using IronWord 的最快方法是创建一个带尺寸的表格对象,实例化一个 WordDocument,使用 AddTable() 添加表格,然后使用 SaveAs() 保存。整个过程只需 4 行代码即可完成。
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 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。