将表格添加到Word文档中 ,包括表格样式设置

创建 Table 对象并设置其属性

在您将表格插入文档之前,必须创建 Table 对象并设置其属性。 要设置表格的属性,请创建TableProperties对象并为其提供值。 TableProperties 类提供许多面向表格的属性,例如Shading 、TableBorders 、TableCaption 、TableCellSpacing 、TableJustification 等等。 示例方法包括以下代码。

 1     Table table = new Table();
 2 
 3     TableProperties props = new TableProperties(
 4         new TableBorders(
 5         new TopBorder
 6         {
 7             Val = new EnumValue<BorderValues>(BorderValues.Single),
 8             Size = 12
 9         },
10         new BottomBorder
11         {
12           Val = new EnumValue<BorderValues>(BorderValues.Single),
13           Size = 12
14         },
15         new LeftBorder
16         {
17           Val = new EnumValue<BorderValues>(BorderValues.Single),
18           Size = 12
19         },
20         new RightBorder
21         {
22           Val = new EnumValue<BorderValues>(BorderValues.Single),
23           Size = 12
24         },
25         new InsideHorizontalBorder
26         {
27           Val = new EnumValue<BorderValues>(BorderValues.Single),
28           Size = 12
29         },
30         new InsideVerticalBorder
31         {
32           Val = new EnumValue<BorderValues>(BorderValues.Single),
33           Size = 12
34     }));
35 
36     table.AppendChild<TableProperties>(props);

TableProperties 类的构造函数允许您指定任意数量的子元素(与 XElement 构造函数很像)。

本例中,代码创建TopBorder 、BottomBorder 、LeftBorder 、RightBorder 、InsideHorizontalBorder 和InsideVerticalBorder 子元素,各子元素分别描述表格一个的边框元素。 对于每个元素,代码在调用构造函数的过程中设置 Val 和 Size 属性。 大小的设置很简单,但是 Val 属性的设置则复杂一点:此特定对象的这个属性表示边框样式,您必须将其设置为枚举值。 为此,请创建 EnumValue 泛型类型的实例,将特定边框类型(Single )作为参数传递给构造函数。 < > 一旦代码已设置它需要设置的所有表格边框值,它将调用 AppendChild<T> 方法的表,指示泛型类型是 TableProperties— 即使用变量 属性 作为值追加 TableProperties 类的实例。

使用数据填充表

有了表格及其属性后,现在应该使用数据填充表格。示例过程首先循环访问您指定的字符串数组中的所有数据行,并为每个数据行创建一个新的 TableRow 实例。下面的代码省去了使用数据填充行的细节,但是演示了如何创建行并将行附加到表格中:

创建11行

1     for (var i = 0; i <=10; i++)
2     {
3         var tr = new TableRow();
4         // Code removed here…
5         table.Append(tr);
6     }

 对于每一列,代码创建一个新的 TableCell 对象,并使用数据填充,然后将其附加到行。下面的代码省去了使用数据填充每个单元格的细节,但是演示了如何创建列并将列附加到表格中:

创建11列

1     for (var j = 0; j <= 10; j++)
2     {
3         var tc = new TableCell();
4         // Code removed here…
5         tr.Append(tc);
6     }

接下来,代码执行以下操作:

  • 新建一个包含字符串中的值的 Text 对象。

  • 将 Text 对象传递给新Run 对象的构造函数。

  • 将 Run 对象传递给Paragraph 对象的构造函数。

    扫描二维码关注公众号,回复: 2984403 查看本文章
  • 将 Paragraph 对象传递给单元格的Append 方法。

换句话说,下面的代码将文本附加到新的 TableCell 对象。

1  tc.Append(new Paragraph(new Run(new Text("数据")));

代码然后将新的 TableCellProperties 对象附加到单元格。 与见过的 TableProperties 对象一样,此 TableCellProperties 对象的构造函数可以接受您提供的任意数量的对象。在本例中,代码仅传递了一个新的TableCellWidth 对象,并将其Type 属性设置为Auto (以便表格自动调整每列的宽度)。

1     // Assume you want columns that are automatically sized.
2     tc.Append(new TableCellProperties(
3         new TableCellWidth { Type = TableWidthUnitValues.Auto }));

完成

下面的代码最后会将表格附加到文档正文,然后保存该文档。

    doc.Body.Append(table);
    doc.Save();

示例代码

 1     // Take the data from a two-dimensional array and build a table at the 
 2     // end of the supplied document.
 3     public static void AddTable(string fileName, string[,] data)
 4     {
 5         using (var document = WordprocessingDocument.Open(fileName, true))
 6         {
 7 
 8             var doc = document.MainDocumentPart.Document;
 9 
10             Table table = new Table();
11 
12             TableProperties props = new TableProperties(
13                 new TableBorders(
14                 new TopBorder
15                 {
16                     Val = new EnumValue<BorderValues>(BorderValues.Single),
17                     Size = 12
18                 },
19                 new BottomBorder
20                 {
21                   Val = new EnumValue<BorderValues>(BorderValues.Single),
22                   Size = 12
23                 },
24                 new LeftBorder
25                 {
26                   Val = new EnumValue<BorderValues>(BorderValues.Single),
27                   Size = 12
28                 },
29                 new RightBorder
30                 {
31                   Val = new EnumValue<BorderValues>(BorderValues.Single),
32                   Size = 12
33                 },
34                 new InsideHorizontalBorder
35                 {
36                   Val = new EnumValue<BorderValues>(BorderValues.Single),
37                   Size = 12
38                 },
39                 new InsideVerticalBorder
40                 {
41                   Val = new EnumValue<BorderValues>(BorderValues.Single),
42                   Size = 12
43             }));
44 
45             table.AppendChild<TableProperties>(props);
46 
47             for (var i = 0; i <= data.GetUpperBound(0); i++)
48             {
49                 var tr = new TableRow();
50                 for (var j = 0; j <= data.GetUpperBound(1); j++)
51                 {
52                     var tc = new TableCell();
53                     tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
54 
55                     // Assume you want columns that are automatically sized.
56                     tc.Append(new TableCellProperties(
57                         new TableCellWidth { Type = TableWidthUnitValues.Auto }));
58 
59                     tr.Append(tc);
60                 }
61                 table.Append(tr);
62             }
63             doc.Body.Append(table);
64             doc.Save();
65         }
66     }

猜你喜欢

转载自www.cnblogs.com/kingline/p/9567259.html