Dealing With Table Borders In OOXML

Formatting tables in a document programmatically can be a very complex task. This is the major reason which we start our document generation projects with templates instead of building components in a document by hand.

Borders are on aspect of a table that you may want to fomat. Borders are used to make certain content in a table stand out. If you need to conditionally set and remove borders there is something that you need to be aware of. Even in OOXML you have the concepts of styles, inheriting styles and overriding styles.

When Word defines a table it will reference a global style such as “TableGrid”. This style will include the borders for the table. Specifically the InsideHorizontalBorder and InsideVerticalBorder define the borders for the cells. These can be overridden by the TableCellBorders collection of a particular cell. Adding a double right border on a cell is as easy as the couple of lines of code below.

If I want to revert back to the table’s style for cell borders I simply need to remove all children from the TableCellBorders collection. It is like removing a class identifier from a TD tag in HTML. The style in the parent object takes back over.

With the knowledge of how the borders work you can take the concept and apply it to other effects of styles.