DOCX
Tables
Create tables with rows, cells, merged cells, borders, and styling
Use Table, TableRow, and TableCell to add structured data to your documents.
Basic Table
import { Table, TableRow, TableCell, Paragraph, TextRun } from "@office-open/docx";
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({ children: [new TextRun("Name")] })],
}),
new TableCell({
children: [new Paragraph({ children: [new TextRun("Age")] })],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph({ children: [new TextRun("Alice")] })],
}),
new TableCell({
children: [new Paragraph({ children: [new TextRun("30")] })],
}),
],
}),
],
});
Column Widths
new Table({
columnWidths: [3000, 2000, 2000],
rows: [
new TableRow({
children: [
new TableCell({
width: { size: 3000, type: "dxa" },
children: [new Paragraph("Col 1")],
}),
new TableCell({
width: { size: 2000, type: "dxa" },
children: [new Paragraph("Col 2")],
}),
new TableCell({
width: { size: 2000, type: "dxa" },
children: [new Paragraph("Col 3")],
}),
],
}),
],
});
Merged Cells
Use verticalMerge to merge cells vertically and columnSpan to merge horizontally:
import { VerticalMergeType } from "@office-open/docx";
new Table({
rows: [
new TableRow({
children: [
// Start a vertical merge
new TableCell({
verticalMerge: VerticalMergeType.RESTART,
children: [new Paragraph("Merged (2 rows)")],
}),
new TableCell({
children: [new Paragraph("B1")],
}),
],
}),
new TableRow({
children: [
// Continue the vertical merge
new TableCell({
verticalMerge: VerticalMergeType.CONTINUE,
children: [new Paragraph()],
}),
new TableCell({
children: [new Paragraph("B2")],
}),
],
}),
],
});
Table Borders
new 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: [
// ...rows
],
});
Cell Formatting
Style individual cells with backgrounds, borders, and vertical alignment:
new TableCell({
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: [new Paragraph("Styled cell")],
});
Table Width Modes
Control how the table occupies the page width:
new Table({
width: { size: 100, type: "pct" }, // 100% of page width
rows: [
// ...rows
],
});
Width types:
"dxa"— Fixed width in TWIPs"pct"— Percentage of page width (50 = 50%)"auto"— Auto-fit to content
Table Options Reference
| Option | Type | Description |
|---|---|---|
rows | TableRow[] | 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 |