Working With Tables (WordprocessingML)

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

This topic discusses the Open XML SDK 2.0Table class and how it relates to the Office Open XML File Formats WordprocessingML schema.

Tables in WordprocessingML

The following text from the ISO/IEC 29500 specification introduces the Open XML WordprocessingML table element.

Another type of block-level content in WordprocessingML, A table is a set of paragraphs (and other block-level content) arranged in rows and columns.

Tables in WordprocessingML are defined via the tbl element, which is analogous to the HTML <table> tag. The table element specifies the location of a table present in the document.

A tbl element has two elements that define its properties: tblPr, which defines the set of table-wide properties (such as style and width), and tblGrid, which defines the grid layout of the table. A tbl element can also contain an arbitrary non-zero number of rows, where each row is specified with a tr element. Each tr element can contain an arbitrary non-zero number of cells, where each cell is specified with a tc element.

© ISO/IEC29500: 2008.

The following table lists some of the most common Open XML SDK classes used when working with tables.

XML element

Open XML SDK 2.0 Class

gridCol

GridColumn

tblGrid

TableGrid

tblPr

TableProperties

tc

TableCell

tr

TableRow

Table Class

The OXML SDK Table class represents the (<tbl>) element defined in the Open XML File Format schema for WordprocessingML documents as discussed above. Use a Tableobject to manipulate an individual table in a WordprocessingML document.

TableProperties Class

The OXML SDK TableProperties class represents the (<tblPr>) element defined in the Open XML File Format schema for WordprocessingML documents. The <tblPr> element defines table-wide properties for a table. Use a TableProperties object to set table-wide properties for a table in a WordprocessingML document.

TableGrid Class

The OXML SDK TableGrid class represents the (<tblGrid>) element defined in the Open XML File Format schema for WordprocessingML documents. In conjunction with grid column (<gridCol>) child elements, the <tblGrid> element defines the columns for a table and specifies the default width of table cells in the columns. Use a TableGrid object to define the columns in a table in a WordprocessingML document.

GridColumn Class

The OXML SDK GridColumn class represents the grid column (<gridCol>) element defined in the Open XML File Format schema for WordprocessingML documents. The <gridCol> element is a child element of the <tblGrid> element and defines a single column in a table in a WordprocessingML document. Use the GridColumn class to manipulate an individual column in a WordprocessingML document.

TableRow Class

The OXML SDK TableRow class represents the table row (<tr>) element defined in the Open XML File Format schema for WordprocessingML documents. The <tr> element defines a row in a table in a WordprocessingML document, analogous to the <tr> tag in HTML. A table row can also have formatting applied to it using a table row properties (<trPr>) element. The Open XML SDK TableRowProperties class represents the <trPr> element.

TableCell Class

The OXML SDK TableCell class represents the table cell (<tc>) element defined in the Open XML File Format schema for WordprocessingML documents. The <tc> element defines a cell in a table in a WordprocessingML document, analogous to the <td> tag in HTML. A table cell can also have formatting applied to it using a table cell properties (<tcPr>) element. The Open XML SDK TableCellProperties class represents the <tcPr> element.

Example

The following code inserts a table with 1 row and 3 columns into a document.

public static void 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.
        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);
    }
}
Public Sub InsertTableInDoc(ByVal filepath As String)
    ' Open a WordprocessingDocument for editing using the filepath.
    Using wordprocessingDocument As WordprocessingDocument = _
        WordprocessingDocument.Open(filepath, True)
        ' Assign a reference to the existing document body.
        Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body

        ' Create a table.
        Dim tbl As New Table()

        ' Set the style and width for the table.
        Dim tableProp As New TableProperties()
        Dim tableStyle As New TableStyle() With {.Val = "TableGrid"}

        ' Make the table width 100% of the page width.
        Dim tableWidth As New TableWidth() With {.Width = "5000", .Type = TableWidthUnitValues.Pct}

        ' Apply
        tableProp.Append(tableStyle, tableWidth)
        tbl.AppendChild(tableProp)

        ' Add 3 columns to the table.
        Dim tg As New TableGrid(New GridColumn(), New GridColumn(), New GridColumn())
        tbl.AppendChild(tg)

        ' Create 1 row to the table.
        Dim tr1 As New TableRow()

        ' Add a cell to each column in the row.
        Dim tc1 As New TableCell(New Paragraph(New Run(New Text("1"))))
        Dim tc2 As New TableCell(New Paragraph(New Run(New Text("2"))))
        Dim tc3 As 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)
    End Using
End Sub

When this code is run, the following XML is written to the WordprocessingML document specified in the preceding code.

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

See Also

Concepts

About the Open XML SDK 2.0 for Microsoft Office

Structure of a WordprocessingML Document

Working With Paragraphs

Working With Runs