表是 Word 中的另一类型的块级内容,它是以行和列排列的一组段落(以及其他块级内容)。

Word 中的表格通过 tbl 元素定义,该元素类似于 HTML <表格>标记。 表元素指定文档中存在的表的位置。

tbl 元素具有两个定义其属性的元素:tblPr 和 tblGrid,前者定义表范围的一组属性(如样式和宽度),后者定义表的网格布局。tbl 元素还可包含任意(非零)数目的行,其中每行使用 tr 元素来指定。每个 tr 元素可包含任意非零个单元格,其中每个单元格使用 tc 元素来指定。

下表列出使用表时使用的一些最常见的 Open XML SDK 类。

XML 元素Open XML SDK 类
内容单元格 内容单元格
gridCol GridColumn
tblGrid TableGrid
tblPr TableProperties
tc TableCell
tr TableRow

Table 类

Open XML SDK  Table 类代表 (<tbl>),该元素在 Word文档的 Open XML 文件格式架构中定义,如上所述。 使用 Table 对象可以处理 Word 文档中的单个表。

TableProperties 类

Open XML SDK TableProperties 类代表 (<tblPr>) 元素,该元素在 Word文档的 Open XML 文件格式架构中定义。 <tblPr> 元素定义表的表范围属性。 使用 TableProperties 对象可为 Word 文档中的表设置表范围属性。

TableGrid 类

Open XML SDK  TableGrid 类代表 (<tblGrid>) 元素,该元素在 Word文档的 Open XML 文件格式架构中定义。 与网格列 (<gridCol>) 子元素一起使用时,<tblGrid> 元素定义表的列并指定列中表格单元格的默认宽度。 使用 TableGrid 对象可以定义 Word 文档的表中的列。

GridColumn 类

Open XML SDK GridColumn 类代表网格列 (<gridCol>) 元素,该元素在 Word文档的 Open XML 文件格式架构中定义。 <gridCol> 元素是 <tblGrid> 元素的一个子元素,并在 Word 文档中的表中定义的单个列。 使用 GridColumn 类可以处理 Word 文档中的单个列。

TableRow 类

Open XML SDK  TableRow 类代表表格行 (<tr>) 元素,该元素在 Word文档的 Open XML 文件格式架构中定义。 <tr> 元素在 Word 文档的表中定义一行,类似于 HTML 中的 <tr> 标记。 使用表格行属性 (<trPr>) 元素,还可对表格行应用格式。 Open XML SDK TableRowProperties 类表示 <trPr> 元素。

TableCell 类

Open XML SDK  TableCell 类代表表格单元格 (<tr>) 元素,该元素在 Word 文档的 Open XML 文件格式架构中定义。 <tc> 元素在 Word文档的表中定义一个单元格,类似于 HTML 中的 <td> 标记。 使用表格单元格属性 (<tcPr>) 元素,还可对表格单元格应用格式。 Open XML SDK TableCellProperties 类表示 <tcPr> 元素。

Open XML SDK 代码示例

下面的代码可在文档中插入具有 10 行和 10 列的表。

 1         public static void InsertTableInDoc(string filepath)
 2         {
 3             using (pkg.WordprocessingDocument doc = pkg.WordprocessingDocument.Open(filepath, true))
 4             {
 5                 Word.Body body = doc.MainDocumentPart.Document.Body;
 6 
 7                 Word.Table tbl = new Word.Table();
 8 
 9                 Word.TableProperties tableProp = new Word.TableProperties();
10 
11                 //设置表样式
12                 Word.TableStyle tableStyle = new Word.TableStyle() { Val = "1" };
13 
14                 Word.TableWidth tableWidth = new Word.TableWidth() { Width = "5000", Type = Word.TableWidthUnitValues.Pct };
15 
16                 tableProp.Append(tableStyle, tableWidth);
17                 tbl.AppendChild(tableProp);
18 
19                 Word.TableGrid tg = new Word.TableGrid(new Word.GridColumn(), new Word.GridColumn(), new Word.GridColumn());
20 
21                 tbl.AppendChild(tg);
22 
23                 Word.TableRow tr1 = null;
24 
25                 for(int r = 1; r <= 10; r++)
26                 {
27                     tr1 = new Word.TableRow();
28                     for (int c = 1; c <= 10; c++)
29                     {
30                         tr1.Append(new Word.TableCell(new Word.Paragraph(new Word.Run(new Word.Text((c*r).ToString())))));
31                     }
32                     tbl.AppendChild(tr1);
33                 }
34                
35                 //  Word.TableCell tc1 = new Word.TableCell(new Word.Paragraph(new Word.Run(new Word.Text("1"))));
36 
37                
38 
39                 body.AppendChild(tbl);
40 
41                 doc.Close();
42                 doc.Dispose();
43             }

 

posted on 2018-08-31 09:05  天之梅子  阅读(1797)  评论(0编辑  收藏  举报