DOCX
Tables
Create tables with rows, cells, merged cells, borders, and styling
Use the table, cells, and children properties to add structured data to your documents.
Basic Table
{
"sections": [
{
"children": [
{
"table": {
"rows": [
{
"cells": [
{ "children": [{ "paragraph": { "children": [{ "text": "Name" }] } }] },
{ "children": [{ "paragraph": { "children": [{ "text": "Age" }] } }] }
]
},
{
"cells": [
{ "children": [{ "paragraph": { "children": [{ "text": "Alice" }] } }] },
{ "children": [{ "paragraph": { "children": [{ "text": "30" }] } }] }
]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
rows: [
{
cells: [
{ children: [{ paragraph: { children: ["Name"] } }] },
{ children: [{ paragraph: { children: ["Age"] } }] },
],
},
{
cells: [
{ children: [{ paragraph: { children: ["Alice"] } }] },
{ children: [{ paragraph: { children: ["30"] } }] },
],
},
],
},
},
],
},
],
});
Column Widths
{
"sections": [
{
"children": [
{
"table": {
"columnWidths": [3000, 2000, 2000],
"rows": [
{
"cells": [
{
"children": [{ "paragraph": { "children": ["Col 1"] } }],
"width": { "size": 3000, "type": "dxa" }
},
{
"children": [{ "paragraph": { "children": ["Col 2"] } }],
"width": { "size": 2000, "type": "dxa" }
},
{
"children": [{ "paragraph": { "children": ["Col 3"] } }],
"width": { "size": 2000, "type": "dxa" }
}
]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
columnWidths: [3000, 2000, 2000],
rows: [
{
cells: [
{
width: { size: 3000, type: "dxa" },
children: [{ paragraph: { children: ["Col 1"] } }],
},
{
width: { size: 2000, type: "dxa" },
children: [{ paragraph: { children: ["Col 2"] } }],
},
{
width: { size: 2000, type: "dxa" },
children: [{ paragraph: { children: ["Col 3"] } }],
},
],
},
],
},
},
],
},
],
});
Merged Cells
Use verticalMerge to merge cells vertically and columnSpan to merge horizontally:
{
"sections": [
{
"children": [
{
"table": {
"rows": [
{
"cells": [
{
"verticalMerge": "RESTART",
"children": [{ "paragraph": { "children": ["Merged (2 rows)"] } }]
},
{ "children": [{ "paragraph": { "children": ["B1"] } }] }
]
},
{
"cells": [
{
"verticalMerge": "CONTINUE",
"children": [{ "paragraph": { "children": [] } }]
},
{ "children": [{ "paragraph": { "children": ["B2"] } }] }
]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
rows: [
{
cells: [
// Start a vertical merge
{
verticalMerge: "RESTART",
children: [{ paragraph: { children: ["Merged (2 rows)"] } }],
},
{ children: [{ paragraph: { children: ["B1"] } }] },
],
},
{
cells: [
// Continue the vertical merge
{
verticalMerge: "CONTINUE",
children: [{ paragraph: { children: [] } }],
},
{ children: [{ paragraph: { children: ["B2"] } }] },
],
},
],
},
},
],
},
],
});
Table Borders
{
"sections": [
{
"children": [
{
"table": {
"borders": {
"top": { "style": "single", "size": 1, "color": "000000" },
"bottom": { "style": "single", "size": 1, "color": "000000" },
"left": { "style": "single", "size": 1, "color": "000000" },
"right": { "style": "single", "size": 1, "color": "000000" },
"insideHorizontal": { "style": "single", "size": 1, "color": "000000" },
"insideVertical": { "style": "single", "size": 1, "color": "000000" }
},
"rows": [
{
"cells": [
{ "children": [{ "paragraph": { "children": ["A"] } }] },
{ "children": [{ "paragraph": { "children": ["B"] } }] }
]
},
{
"cells": [
{ "children": [{ "paragraph": { "children": ["C"] } }] },
{ "children": [{ "paragraph": { "children": ["D"] } }] }
]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
borders: {
top: { style: "single", size: 1, color: "000000" },
bottom: { style: "single", size: 1, color: "000000" },
left: { style: "single", size: 1, color: "000000" },
right: { style: "single", size: 1, color: "000000" },
insideHorizontal: { style: "single", size: 1, color: "000000" },
insideVertical: { style: "single", size: 1, color: "000000" },
},
rows: [
{
cells: [
{ children: [{ paragraph: { children: ["A"] } }] },
{ children: [{ paragraph: { children: ["B"] } }] },
],
},
{
cells: [
{ children: [{ paragraph: { children: ["C"] } }] },
{ children: [{ paragraph: { children: ["D"] } }] },
],
},
],
},
},
],
},
],
});
Cell Formatting
Style individual cells with backgrounds, borders, and vertical alignment:
{
"sections": [
{
"children": [
{
"table": {
"rows": [
{
"cells": [
{
"shading": { "fill": "F2F2F2" },
"verticalAlign": "center",
"borders": {
"top": { "style": "single", "size": 1, "color": "CCCCCC" },
"bottom": { "style": "single", "size": 1, "color": "CCCCCC" },
"left": { "style": "single", "size": 1, "color": "CCCCCC" },
"right": { "style": "single", "size": 1, "color": "CCCCCC" }
},
"children": [{ "paragraph": { "children": ["Styled cell"] } }]
}
]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
rows: [
{
cells: [
{
shading: { fill: "F2F2F2" },
verticalAlign: "center",
borders: {
top: { style: "single", size: 1, color: "CCCCCC" },
bottom: { style: "single", size: 1, color: "CCCCCC" },
left: { style: "single", size: 1, color: "CCCCCC" },
right: { style: "single", size: 1, color: "CCCCCC" },
},
children: [{ paragraph: { children: ["Styled cell"] } }],
},
],
},
],
},
},
],
},
],
});
Table Width Modes
Control how the table occupies the page width:
{
"sections": [
{
"children": [
{
"table": {
"width": { "size": 100, "type": "pct" },
"rows": [
{
"cells": [{ "children": [{ "paragraph": { "children": ["Full width table"] } }] }]
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
table: {
width: { size: 100, type: "pct" }, // 100% of page width
rows: [
{
cells: [{ children: [{ paragraph: { children: ["Full width table"] } }] }],
},
],
},
},
],
},
],
});
Width types:
"dxa"— Fixed width in TWIPs"pct"— Percentage of page width (100 = 100%)"auto"— Auto-fit to content
Table Options Reference
| Option | Type | Description |
|---|---|---|
rows | TableRowOptions[] | Table rows |
width | object | Table width ({ size, type }) |
columnWidths | number[] | Width for each column in TWIPs |
borders | object | Border definitions for all edges |
float | object | Floating table positioning |