DOCX
表格
创建包含行、单元格、合并单元格、边框和样式的表格
使用 table、cells 和 children 属性向文档中添加结构化数据。
基本表格
{
"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"] } }] },
],
},
],
},
},
],
},
],
});
列宽
{
"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"] } }],
},
],
},
],
},
},
],
},
],
});
合并单元格
使用 verticalMerge 进行垂直合并,使用 columnSpan 进行水平合并:
{
"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: [
// 开始垂直合并
{
verticalMerge: "RESTART",
children: [{ paragraph: { children: ["Merged (2 rows)"] } }],
},
{ children: [{ paragraph: { children: ["B1"] } }] },
],
},
{
cells: [
// 继续垂直合并
{
verticalMerge: "CONTINUE",
children: [{ paragraph: { children: [] } }],
},
{ children: [{ paragraph: { children: ["B2"] } }] },
],
},
],
},
},
],
},
],
});
表格边框
{
"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"] } }] },
],
},
],
},
},
],
},
],
});
单元格格式
为单个单元格设置背景、边框和垂直对齐:
{
"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"] } }],
},
],
},
],
},
},
],
},
],
});
表格宽度模式
控制表格如何占用页面宽度:
{
"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%
rows: [
{
cells: [{ children: [{ paragraph: { children: ["Full width table"] } }] }],
},
],
},
},
],
},
],
});
宽度类型:
"dxa"— 固定宽度(TWIP)"pct"— 页面宽度百分比(100 = 100%)"auto"— 自动适应内容
Table 选项参考
| 选项 | 类型 | 说明 |
|---|---|---|
rows | TableRowOptions[] | 表格行 |
width | object | 表格宽度({ size, type }) |
columnWidths | number[] | 各列宽度(TWIP) |
borders | object | 所有边的边框定义 |
float | object | 浮动表格定位 |