将表格添加到字处理文档中
本主题演示如何使用 Open XML SDK for Office 中的类以编程方式向字处理文档添加表。 它包含演示此任务的示例 AddTable
方法。
AddTable 方法
可以使用 AddTable
方法向字处理文档添加简单表。 方法 AddTable
接受两个参数,指示以下内容:
要修改的文档的名称(字符串)。
要作为表格插入文档中的字符串二维数组。
static void AddTable(string fileName, string[,] data)
调用 AddTable 方法
方法 AddTable
修改指定的文档,添加一个表,其中包含您提供的二维数组中的信息。 要调用此方法,请同时传递这两个参数值,如下面的代码所示。
string fileName = args[0];
AddTable(fileName, new string[,] {
{ "Hawaii", "HI" },
{ "California", "CA" },
{ "New York", "NY" },
{ "Massachusetts", "MA" }
});
代码的工作原理
以下代码首先打开文档,使用 Open 方法,并指示应打开文档以 (最终 true
参数值) 进行读/写访问。 接下来,代码使用Document字处理文档的 属性检索对 main 文档部件的MainDocumentPart根元素的引用。
using (var document = WordprocessingDocument.Open(fileName, true))
{
if (document.MainDocumentPart is null || document.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
var doc = document.MainDocumentPart.Document;
创建表对象并设置其属性
必须先创建 Table 对象并设置其属性,然后才能将表格插入文档。 若要设置表的属性,请为 TableProperties 对象创建并提供值。 类TableProperties
提供许多面向表的属性,如 Shading、TableBorders、TableCaption、TableCellPropertiesTableJustification、 等。 示例方法包括以下代码。
Table table = new();
TableProperties props = new(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild<TableProperties>(props);
类的 TableProperties
构造函数允许指定任意数量的子元素, (与构造函数) 非常类似 XElement 。 在这种情况下,代码将创建 TopBorder、、BottomBorder、LeftBorderRightBorder、InsideHorizontalBorder、 和 InsideVerticalBorder 子元素,每个元素描述表的边框元素之一。 对于每个元素,代码将 和 Size
属性设置为Val
调用构造函数的一部分。 设置大小很简单,但设置 Val
属性需要多一点工作:对于此特定对象,此属性表示边框样式,必须将其设置为枚举值。 为此,请创建泛型类型的 EnumValue<T> 实例,将特定边框类型 (BorderValues) 作为参数传递给构造函数。 一旦代码设置了它需要设置的所有表边框值,它将调用AppendChild表的 方法,指示泛型类型是TableProperties,即,它使用 变量props
作为值追加 类的TableProperties
实例。
用数据填充表
有了表格及其属性后,现在应该使用数据填充表格。 示例过程首先循环访问指定的字符串数组中的所有数据行,为每行数据创建一个新 TableRow 实例。 以下代码演示如何创建行并将其追加到表。 然后,对于每一列,代码将创建一个新 TableCell 对象,用数据填充该对象,并将其追加到行中。
接下来,代码执行以下操作:
- 创建一个新的 Text 对象,该对象包含字符串数组中的值。
- 将 Text 对象传递给新 Run 对象的构造函数。
- 将 Run 对象传递给新 Paragraph 对象的构造函数。
- 将 Paragraph 对象传递给 Append 单元格的 方法。
然后,代码会将新 TableCellProperties 对象追加到单元格中。 此 TableCellProperties
对象(与你已看到的对象一样 TableProperties
)可以接受其构造函数中的任意数量的对象,而你愿意提供的对象。 在这种情况下,代码仅传递一个新 TableCellWidth 对象,其 Type 属性设置为 TableWidthUnitValues (,以便表自动调整每个列的宽度) 。
for (var i = 0; i < data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j < data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
完成
下面的代码最后会将表格附加到文档正文,然后保存该文档。
doc.Body.Append(table);
示例代码
下面是 C# 和 Visual Basic 中的完整 AddTable 示例代码。
static void AddTable(string fileName, string[,] data)
{
if (data is not null)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{
if (document.MainDocumentPart is null || document.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
var doc = document.MainDocumentPart.Document;
Table table = new();
TableProperties props = new(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild<TableProperties>(props);
for (var i = 0; i < data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j < data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
}
}
}