使用 WordprocessingML 表

本主题讨论 Open XML SDK Table 类及其与 Office Open XML 文件格式 WordprocessingML 架构的关系。

WordprocessingML 中的表

ISO/IEC 29500 规范中的以下文本介绍了 Open XML WordprocessingML 表元素。

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

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

元素有两个 tbl 定义其属性的元素: tblPr定义表范围属性集 ((如样式和宽度) )和 tblGrid(用于定义表的网格布局)。 元素 tbl 还可以包含任意非零行数,其中每行都用元素 tr 指定。 每个 tr 元素可以包含任意数量的非零个单元格,其中每个单元格都用元素 tc 指定。

© ISO/IEC 29500:2016

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

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

Open XML SDK 表类

Open XML SDK Table 类表示 <tbl> 在 WordprocessingML 文档的 Open XML 文件格式架构中定义的元素,如上所述。 使用 Table 对象可操作 WordprocessingML 文档中的单个表。

TableProperties 类

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

TableGrid 类

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

GridColumn 类

Open XML SDK GridColumn 类表示在 WordprocessingML 文档的 Open XML 文件格式架构中定义的网格列 <gridCol> 元素。 元素 <gridCol> 是 元素的子元素, <tblGrid> 在 WordprocessingML 文档中的表中定义单个列。 使用 GridColumn 类可操作 WordprocessingML 文档中的单个列。

TableRow 类

Open XML SDK TableRow 类表示在 WordprocessingML 文档的 Open XML 文件格式架构中定义的表行 <tr> 元素。 元素 <tr> 在 WordprocessingML 文档中的表中定义一行,类似于 <tr> HTML 中的 标记。 表行还可以使用表行属性 <trPr> 元素来应用格式设置。 Open XML SDK TableRowProperties 类表示 <trPr> 元素。

TableCell 类

Open XML SDK TableCell 类表示 WordprocessingML 文档的 Open XML 文件格式架构中定义的表单元格 <tc> 元素。 元素 <tc> 在 WordprocessingML 文档中的表中定义单元格,类似于 <td> HTML 中的 标记。 表格单元格还可以使用表格单元格属性 <tcPr> 元素来应用格式。 Open XML SDK TableCellProperties 类表示 <tcPr> 元素。

Open XML SDK 代码示例

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

static string InsertTableInDoc(string filepath)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body or add one if necessary.

        if (wordprocessingDocument.MainDocumentPart is null)
        {
            wordprocessingDocument.AddMainDocumentPart();
        }

        if (wordprocessingDocument.MainDocumentPart!.Document is null)
        {
            wordprocessingDocument.MainDocumentPart.Document = new Document();
        }

        if (wordprocessingDocument.MainDocumentPart.Document.Body is null)
        {
            wordprocessingDocument.MainDocumentPart.Document.Body = new Body();
        }

        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Create a table.
        Table tbl = new Table();

        // Set the style and width for the table.
        TableProperties tableProp = new TableProperties();
        TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

        // Make the table width 100% of the page width.
        TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };

        // Apply
        tableProp.Append(tableStyle, tableWidth);
        tbl.AppendChild(tableProp);

        // Add 3 columns to the table.
        TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
        tbl.AppendChild(tg);

        // Create 1 row to the table.
        TableRow tr1 = new TableRow();

        // Add a cell to each column in the row.
        TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1"))));
        TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2"))));
        TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3"))));
        tr1.Append(tc1, tc2, tc3);

        // Add row to the table.
        tbl.AppendChild(tr1);

        // Add the table to the document
        body.AppendChild(tbl);

        return tbl.LocalName;
    }
}

运行此代码时,以下 XML 会写入前面代码中指定的 WordprocessingML 文档。

<w:tbl>
  <w:tblPr>
    <w:tblStyle w:val="TableGrid" />
    <w:tblW w:w="5000" w:type="pct" />
  </w:tblPr>
  <w:tblGrid>
    <w:gridCol />
    <w:gridCol />
    <w:gridCol />
  </w:tblGrid>
  <w:tr>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>1</w:t>
        </w:r>
      </w:p>
    </w:tc>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>2</w:t>
        </w:r>
      </w:p>
    </w:tc>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>3</w:t>
        </w:r>
      </w:p>
    </w:tc>
  </w:tr>
</w:tbl>

另请参阅