次の方法で共有


WordprocessingML のテーブルを操作する

このトピックでは、Open XML SDK Table クラスと、Office Open XML ファイル形式 WordprocessingML スキーマとの関係について説明します。

WordprocessingML のテーブル

Open XML WordprocessingML のテーブル要素は、ISO/IEC 29500 仕様書の次の文面で導入されています。

WordprocessingML のもう 1 つの種類のブロック レベル コンテンツであるテーブルは、行と列に配置された一連の段落 (および他のブロック レベル コンテンツ) です。

WordprocessingML のテーブルは tbl 要素を介して定義されます。これは HTML <table> タグに似ています。 テーブル要素では、ドキュメントに存在するテーブルの位置を指定します。

tbl要素には、そのプロパティを定義する 2 つの要素があります。tblPrは、テーブル全体のプロパティのセット (スタイルや幅など) を定義し、テーブルのグリッド レイアウトを定義するtblGridです。 tbl要素には、任意の 0 以外の行数を含めることもできます。ここで、各行は tr 要素で指定されます。 各 tr 要素には、セルの任意の 0 以外の数を含めることができます。各セルは、 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 Table Class

Open XML SDK Table クラスは、上記の WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されている <tbl> 要素を表します。 WordprocessingML ドキュメント内の個々のテーブルを操作するには、Table オブジェクトを使用します。

TableProperties クラス

Open XML SDK TableProperties クラスは、WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されている <tblPr> 要素を表します。 <tblPr>要素は、テーブルのテーブル全体のプロパティを定義します。 WordprocessingML ドキュメントのテーブル全体のプロパティを設定するには、TableProperties オブジェクトを使用します。

TableGrid クラス

Open XML SDK TableGrid クラスは、WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されている <tblGrid> 要素を表します。 <tblGrid> 要素は、子要素<gridCol>グリッド列と組み合わせて、テーブルの列を定義し、列内のテーブル セルの既定の幅を指定します。 WordprocessingML ドキュメントのテーブルの列を定義するには、TableGrid オブジェクトを使用します。

GridColumn クラス

Open XML SDK GridColumn クラスは、WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されているグリッド列 <gridCol> 要素を表します。 <gridCol>要素は、<tblGrid> 要素の子要素であり、WordprocessingML ドキュメント内のテーブル内の 1 つの列を定義します。 GridColumn クラスを使用して、WordprocessingML ドキュメント内の個々の列を操作します。

TableRow クラス

Open XML SDK TableRow クラスは、WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されているテーブル行 <tr> 要素を表します。 <tr>要素は、WordprocessingML ドキュメント内のテーブル内の行を定義します。HTML の<tr> タグに似ています。 テーブル行には、テーブル行プロパティ <trPr> 要素を使用して書式設定を適用することもできます。 Open XML SDK TableRowProperties クラスは、 <trPr> 要素を表します。

TableCell クラス

Open XML SDK TableCell クラスは、WordprocessingML ドキュメントの Open XML ファイル形式スキーマで定義されているテーブル セル <tc> 要素を表します。 <tc>要素は、WordprocessingML ドキュメント内のテーブル内のセルを定義します。これは HTML の<td> タグに似ています。 また、テーブル セルには、要素 <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;
    }
}

このコードが実行されると、前のコードで指定した WordprocessingML ドキュメントに次の XML が書き込まれます。

<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>

関連項目