addTable

addTable

BASIC / ADVANCED / PREMIUM

Inserts a table into a Word document.

Description
public addTable ( array $tableData [, array $tableProperties = array() [, array $rowProperties = array()]] )

This method allows the insertion of a table into your Word document.

You may:

  • Create tables with complex row and col spans.
  • Insert in each single table cell:
    • plain text
    • rich text
    • nested tables
    • lists
    • images
    • charts
    • footnotes and endnotes
    • any combination of all of the above
  • Style the table as a whole and/or its individual rows and cells: set, for example, global properties like borders and margins that can be overriden for each table cell.
  • You may position the table by its own or make it float so the surrounding text will wrap around it.

Like in the standard MS Word interface, tables, by default, have a black border in every cell. If you want a borderless table you should specify the border option to none in $tableProperties

Parameters

tableData

This is an array of arrays. Each entry of this array of arrays contains the content and style needed to render that particular table cell.

These entries may be:

  • a simple string of text
  • a WordFragment object
  • itself an array

The first option is only valid for very simple tables that only contain plain text.

The WordFragments are only needed if you want to insert images, charts, nested tables or complex paragraphs.

The array option gives you whole control over the contents and formatting of a particular table cell.

The possible keys and values of each of these arrays are:

Key Type Description
value mixed The text to be inserted (string) or a Word fragment if you want to insert complex paragraphs, images, nested tables...
backgroundColor string Hexadecimal color value: 'FF0000', '000000'...
border string Border type: none, single, double, dashed, threeDEngrave, threeDEmboss, outset, inset... This value can be overridden for each side with the use 'borderTop', 'borderRight', 'borderBottom' and 'borderLeft' properties.
borderColor string Hexadecimal color value: 'FF0000', '000000'... This value can be overridden for each side with the use of 'borderTopColor', 'borderRightColor', 'borderBottomColor' and 'borderLeftColor' properties.
borderSpacing int Border spacing in points. This value can be overridden for each side with the use of 'borderTopSpacing', 'borderRightSpacing', 'borderBottomSpacing' and 'borderLeftSpacing' properties.
borderWidth int Border size in eigths of a point. This value can be overridden for each side with the use of 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth' and 'borderLeftWidth' properties.
cellMargin mixed An integer or an array with keys: top, right, bottom and left. The values are given in twentieths of a point.
colspan int The col span of that particular table cell.
fitText bool If true fits the text to the size of the cell.
noWrap bool If true text does not wrap.
rowspan int The row span of that particular table cell.
textDirection string Sets the direction of the flow of text. Available values are: lr (default), tbRl and btLr.
vAlign string Vertical align of text: top, center, both or bottom.
width int Preferred cell width in twentieths of a point (twips).

tableProperties

This is an array including all general table options (some of them may be overriden by row or cell properties).

The possible keys and values of this array are:

Key Type Description
bidi bool Sets to 'true' for right to left languages (default value is 'false').
border string Border type: none, single, double, dashed, threeDEngrave, threeDEmboss, outset, inset... By default the border type is single. You should select none for borderless tables.
borderColor string Hexadecimal color value: 'FF0000', '000000'...
borderSpacing int Border spacing in points.
borderWidth int Border size in eigths of a point.
borderSettings string Possible values are: all, outside, inside. If all (default value) the border styles apply to all table borders. If the value is set to outside or inside the border styles will only apply to the outside or inside boreders respectively.
cantSplitRows bool Sets global row split properties (can be overriden or set for individual rows by rowProperties).
cellMargin mixed General cell margins for the whole table. An integer or an array with keys: top, right, bottom and left. The values are given in twentieths of a point.
cellSpacing int Separation among cells. Given in twentieths of a point (twips).
columnWidths mixed An integer or a zero-based array with the widths of each column in twentieths of a point.
conditionalFormatting array This option lets you control which formatting conditions declared in the corresponding table style should be applied. The possible keys and values are:
  • 'firstRow' (boolean) first table row conditional formatting.
  • 'lastRow' (boolean) last table row conditional formatting.
  • 'firstCol' (boolean) first table column conditional formatting.
  • 'lastCol' (boolean) last table column conditional formatting.
  • 'noHBand' (boolean) does not apply row banding conditional formatting.
  • 'noVBand' (boolean) does not apply column banding conditional formatting.
The default values are: firstRow (true), firstCol (true), noVBand (true) and all other false.
float array With the following keys and values:
  • 'align' (string) possible values are: left, center, right, outside, inside.
  • 'textMarginTop' (int) in twentieths of a point.
  • 'textMarginRight' (int) in twentieths of a point.
  • 'textMarginBottom' (int) in twentieths of a point.
  • 'textMarginLeft' (int) in twentieths of a point.
font string Font family: 'Arial', Calibri'...
indent int Left table margin in twentieths of a point (twips).
tableAlign string Table alignment. Possible values are: center, left or right.
tableLayout string Possible values are: fixed or autofit (default). Set it to 'fixed' only if you do not want Word to handle the best possible width.
tableStyle string Word table style id (you may run parseStyles method to check for the available styles in phpdocx or your particular template).
tableWidth array Its possible keys and values are:
  • 'type' (pct, dxa) pct if the value refers to percentage and dxa if the value is given in twentieths of a point (twips).
  • 'value' (int).
textProperties array It may include any of the paragraph properties of the addText method so you can completely customize the properies of the text content of the table.

rowProperties

This is an array of arrays (one for each row you want to customize) including all available row options.

The possible keys and values of each of these arrays are:

Key Type Description
cantSplit bool If true the row can not split across pages.
height int Exact row height in twentieths of a point (twips).
minHeight int Minimum row height in twentieths of a point (twips).
tableHeader bool If true, and the table splits across pages, this row repeats at the beginning of each new page.
Code samples

Example #1

The resulting Word document looks like:

Example #2

The resulting Word document looks like:

Example #3

The resulting Word document looks like: