1. 程式人生 > >將表格添加到Word文檔中 ,包括表格樣式設置

將表格添加到Word文檔中 ,包括表格樣式設置

建行 new 設置 automatic 必須 允許 即使 ble otto

創建 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 對象的構造函數。

  • 將 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     }

將表格添加到Word文檔中 ,包括表格樣式設置