Export (0) Print
Expand All
Expand Minimize

How to: Add tables to word processing documents (Open XML SDK)

This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically add a table to a word processing document. It contains an example AddTable method to illustrate this task.

Last modified: March 22, 2013

Applies to: Office 2013 | Open XML

In this article
AddTable Method
Calling the AddTable Method
How the Code Works
Creating the Table Object and Setting Its Properties
Filling the Table with Data
Finishing Up
Sample Code

To use the sample code in this topic, you must install the Open XML SDK 2.5. You must explicitly reference the following assemblies in your project:

  • WindowsBase

  • DocumentFormat.OpenXml (installed by the Open XML SDK)

You must also use the following using directives or Imports statements to compile the code in this topic.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

You can use the AddTable method to add a simple table to a word processing document. The AddTable method accepts two parameters, indicating the following:

  • The name of the document to modify (string).

  • A two-dimensional array of strings to insert into the document as a table.

public static void AddTable(string fileName, string[,] data)

The AddTable method modifies the document you specify, adding a table that contains the information in the two-dimensional array that you provide. To call the method, pass both of the parameter values, as shown in the following code.

const string fileName = @"C:\Users\Public\Documents\AddTable.docx";
AddTable(fileName, new string[,] 
    { { "Texas", "TX" }, 
    { "California", "CA" }, 
    { "New York", "NY" }, 
    { "Massachusetts", "MA" } }
    );

The following code starts by opening the document, using the WordprocessingDocument.Open method and indicating that the document should be open for read/write access (the final true parameter value). Next the code retrieves a reference to the root element of the main document part, using the Document property of the MainDocumentPart of the word processing document.

using (var document = WordprocessingDocument.Open(fileName, true))
{
    var doc = document.MainDocumentPart.Document;
    // Code removed here…
}

Before you can insert a table into a document, you must create the Table object and set its properties. To set a table's properties, you create and supply values for a TableProperties object. The TableProperties class provides many table-oriented properties, like Shading, TableBorders, TableCaption, TableCellSpacing, TableJustification, and more. The sample method includes the following code.

Table table = new Table();

TableProperties props = new TableProperties(
    new TableBorders(
    new TopBorder
    {
        Val = new EnumValue<BorderValues>(BorderValues.Single),
        Size = 12
    },
    new BottomBorder
    {
      Val = new EnumValue<BorderValues>(BorderValues.Single),
      Size = 12
    },
    new LeftBorder
    {
      Val = new EnumValue<BorderValues>(BorderValues.Single),
      Size = 12
    },
    new RightBorder
    {
      Val = new EnumValue<BorderValues>(BorderValues.Single),
      Size = 12
    },
    new InsideHorizontalBorder
    {
      Val = new EnumValue<BorderValues>(BorderValues.Single),
      Size = 12
    },
    new InsideVerticalBorder
    {
      Val = new EnumValue<BorderValues>(BorderValues.Single),
      Size = 12
}));

table.AppendChild<TableProperties>(props);

The constructor for the TableProperties class allows you to specify as many child elements as you like (much like the XElement constructor). In this case, the code creates TopBorder, BottomBorder, LeftBorder, RightBorder, InsideHorizontalBorder, and InsideVerticalBorder child elements, each describing one of the border elements for the table. For each element, the code sets the Val and Size properties as part of calling the constructor. Setting the size is simple, but setting the Val property requires a bit more effort: this property, for this particular object, represents the border style, and you must set it to an enumerated value. To do that, you create an instance of the EnumValue<T> generic type, passing the specific border type (Single) as a parameter to the constructor. Once the code has set all the table border value it needs to set, it calls the AppendChild<T> method of the table, indicating that the generic type is TableProperties—that is, it is appending an instance of the TableProperties class, using the variable props as the value.

Given that table and its properties, now it is time to fill the table with data. The sample procedure iterates first through all the rows of data in the array of strings that you specified, creating a new TableRow instance for each row of data. The following code leaves out the details of filling in the row with data, but it shows how you create and append the row to the table:

for (var i = 0; i <= data.GetUpperBound(0); i++)
{
    var tr = new TableRow();
    // Code removed here…
    table.Append(tr);
}

For each row, the code iterates through all the columns in the array of strings you specified. For each column, the code creates a new TableCell object, fills it with data, and appends it to the row. The following code leaves out the details of filling each cell with data, but it shows how you create and append the column to the table:

for (var j = 0; j <= data.GetUpperBound(1); j++)
{
    var tc = new TableCell();
    // Code removed here…
    tr.Append(tc);
}

Next, the code does the following:

  • Creates a new Text object that contains a value from the array of strings.

  • Passes the Text object to the constructor for a new Run object.

  • Passes the Run object to the constructor for a new Paragraph object.

  • Passes the Paragraph object to the Append method of the cell.

In other words, the following code appends the text to the new TableCell object.

tc.Append(new Paragraph(new Run(new Text(data[i, j]))));

The code then appends a new TableCellProperties object to the cell. This TableCellProperties object, like the TableProperties object you already saw, can accept as many objects in its constructor as you care to supply. In this case, the code passes only a new TableCellWidth object, with its Type property set to Auto (so that the table automatically sizes the width of each column).

// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
    new TableCellWidth { Type = TableWidthUnitValues.Auto }));

The following code concludes by appending the table to the body of the document, and then saving the document.

doc.Body.Append(table);
doc.Save();

The following is the complete AddTable code sample in C# and Visual Basic.

// Take the data from a two-dimensional array and build a table at the 
// end of the supplied document.
public static void AddTable(string fileName, string[,] data)
{
    using (var document = WordprocessingDocument.Open(fileName, true))
    {

        var doc = document.MainDocumentPart.Document;

        Table table = new Table();

        TableProperties props = new TableProperties(
            new TableBorders(
            new TopBorder
            {
                Val = new EnumValue<BorderValues>(BorderValues.Single),
                Size = 12
            },
            new BottomBorder
            {
              Val = new EnumValue<BorderValues>(BorderValues.Single),
              Size = 12
            },
            new LeftBorder
            {
              Val = new EnumValue<BorderValues>(BorderValues.Single),
              Size = 12
            },
            new RightBorder
            {
              Val = new EnumValue<BorderValues>(BorderValues.Single),
              Size = 12
            },
            new InsideHorizontalBorder
            {
              Val = new EnumValue<BorderValues>(BorderValues.Single),
              Size = 12
            },
            new InsideVerticalBorder
            {
              Val = new EnumValue<BorderValues>(BorderValues.Single),
              Size = 12
        }));

        table.AppendChild<TableProperties>(props);

        for (var i = 0; i <= data.GetUpperBound(0); i++)
        {
            var tr = new TableRow();
            for (var j = 0; j <= data.GetUpperBound(1); j++)
            {
                var tc = new TableCell();
                tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
                
                // Assume you want columns that are automatically sized.
                tc.Append(new TableCellProperties(
                    new TableCellWidth { Type = TableWidthUnitValues.Auto }));
                
                tr.Append(tc);
            }
            table.Append(tr);
        }
        doc.Body.Append(table);
        doc.Save();
    }
}

Contribute to this article

Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.

Show:
© 2014 Microsoft