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.
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.
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.
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 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:
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.
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).
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |