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

如何使用 IronWord 在 DOCX 中使用 C&#35 添加表格。

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

IronWord 使开发人员能够通过创建具有指定行和列的 Table 对象,使用边框和颜色对其进行样式设置,并在保存为 DOCX 文件之前将内容填充到单元格中,从而以 C# 编程方式将表格添加到 Word 文档中。

<! -- 待办事项:在此处添加图片 --> <! --介绍实现的示意图 --> <!--说明:说明代码概念的图表或截图 -->

快速入门:一次调用即可创建并保存表格

本示例演示了如何在 IronWord 中创建表格。 使用尺寸构建表格,应用样式,添加内容,将其插入文档并保存。 您可以在几分钟内生成带有样式表的 DOCX 文件。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronWord

    PM > Install-Package IronWord

  2. 复制并运行这段代码。

    var table = new IronWord.Models.Table(3,4);
    var doc = new IronWord.WordDocument();
    doc.AddTable(table);
    doc.SaveAs("QuickTable.docx");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronWord,免费试用!
    arrow pointer

如何在 Word 文档中添加表格?

表格是 Word 文档的基本组成部分。 首先,通过提供行数和列数实例化 Table 类。 配置表格的样式,包括背景颜色、阴影、边框、斑马线和宽度。 其次,使用直观的 [row, column] 索引访问每个单元格。 在每个单元格中添加文本、图像、形状、段落甚至表格。 最后,将表格添加到 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")
$vbLabelText   $csharpLabel
Word文档,显示带有编号、名、姓列和交替行阴影的空4x3表格

TableCell 类的 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
$vbLabelText   $csharpLabel

我可以对表格应用哪些样式选项?

IronWord 为表格提供了广泛的样式设计功能,可以创建视觉上吸引人的专业文档。 除了基本的边框和颜色外,还可以控制单元格填充、对齐,并通过斑马线应用条件格式。 样式系统将强大功能与直观设计相结合,使用熟悉的属性名称和清晰的值枚举。

哪些边框样式可用?

使用 BorderValues 枚举探索边框值的所有可用选项:

Word边框样式菜单,显示单边框、双边框、三边框、点边框、虚线边框、波浪边框以及各种厚度组合等选项

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")
$vbLabelText   $csharpLabel

表宽和对齐属性提供了额外的布局控制。 将表格设置为特定宽度或百分比,在文档中对齐,并控制与周围内容的交互。 单元格级样式选项包括单独的背景颜色、文本对齐方式和填充调整,可对表格外观的各个方面进行细化控制。

通过这些样式选项,可以创建符合任何文档设计要求的表格,从简单的数据网格到具有多个可视化层次结构的复杂财务报表。

常见问题解答

如何在 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 行代码即可完成。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 32,629 | 版本: 2026.2 刚刚发布