Create beautiful, full-width tables in Word documents using the docx npm library. Tables are properly sized, centered, and styled. Use when you need to create DOCX files with professional-looking tables like itineraries, schedules, data tables, or reports.
在Word文档中创建美观的表格,并确保其在Word和Google Docs中表现一致。
经过大量测试,以下是确保表格在所有平台正确渲染的必备模式:
每个表格需要在两个位置设置宽度——表格本身以及每个单元格:
javascript
// 表格级别
new Table({
width: { size: 9360, type: WidthType.DXA }, // 总宽度
columnWidths: [1872, 7488], // 每列宽度
rows: [...]
})
// 单元格级别 - 每个单元格都需要!
new TableCell({
width: { size: 1872, type: WidthType.DXA }, // 该单元格宽度
children: [...]
})
如果缺少其中任何一个,表格在某些平台上会渲染不正确。
百分比在Google Docs中会出错。始终使用DXA(缇):
javascript
// ❌ 错误 - 在Google Docs中出错
width: { size: 100, type: WidthType.PERCENTAGE }
// ✅ 正确
width: { size: 9360, type: WidthType.DXA }
这是一个微妙但关键的陷阱:
javascript
const { ShadingType } = require(docx);
// ❌ 错误 - 产生黑色背景
shading: { type: ShadingType.SOLID, fill: E0F2F1 }
// ✅ 正确 - 应用填充颜色
shading: { type: ShadingType.CLEAR, fill: E0F2F1 }
防止文字紧贴边框:
javascript
const cellMargins = { top: 80, bottom: 80, left: 120, right: 120 };
new TableCell({
children: [...],
margins: cellMargins
})
对于带1英寸页边距的美国信纸:9360 DXA
javascript
// 2列:20% + 80%
columnWidths: [1872, 7488] // = 9360 ✓
// 3列:等宽
columnWidths: [3120, 3120, 3120] // = 9360 ✓
// 3列:25% + 25% + 50%
columnWidths: [2340, 2340, 4680] // = 9360 ✓
javascript
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
WidthType, AlignmentType, BorderStyle, TableLayoutType, ShadingType } = require(docx);
const fs = require(fs);
// 常量
const TOTAL_WIDTH = 9360; // 带1英寸页边距的美国信纸
const TIME_COL = 1872; // 20%
const CONTENT_COL = 7488; // 80%
// 浅灰色边框
const cellBorders = {
top: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
bottom: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
left: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
right: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC }
};
// 单元格内边距
const cellMargins = { top: 80, bottom: 80, left: 120, right: 120 };
// 表格辅助函数
function table(columnWidths, rows) {
return new Table({
layout: TableLayoutType.FIXED,
width: { size: columnWidths.reduce((a, b) => a + b, 0), type: WidthType.DXA },
columnWidths: columnWidths,
rows: rows
});
}
// 表头行
function headerRow(text, color) {
return new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text, bold: true, size: 32, color: FFFFFF })],
alignment: AlignmentType.CENTER
})],
width: { size: TIMECOL + CONTENTCOL, type: WidthType.DXA },
columnSpan: 2,
shading: { type: ShadingType.CLEAR, fill: color },
borders: cellBorders,
margins: cellMargins
})
]
});
}
// 数据行
function dataRow(time, activity, color) {
return new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: time, bold: true, size: 26, color })],
alignment: AlignmentType.CENTER
})],
width: { size: TIME_COL, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
}),
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: activity, size: 26 })]
})],
width: { size: CONTENT_COL, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
})
]
});
}
// 构建文档
const doc = new Document({
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } }
},
children: [
table([TIMECOL, CONTENTCOL], [
headerRow(日程安排, 1565C0),
dataRow(9:00 AM, 到达目的地, 1565C0),
dataRow(10:00 AM, 上午活动, 1565C0),
dataRow(12:00 PM, 午餐休息, 1565C0),
])
]
}]
});
Packer.toBuffer(doc).then(buffer => {
fs.writeFileSync(output.docx, buffer);
});
对于美国信纸(8.5英寸)带1英寸页边距 = 9360 DXA:
| 布局 | 列宽 | 总和 |
|---|---|---|
| 2列(20/80) | [1872, 7488] | 9360 |
| 2列(25/75) |
❌ 缺少单元格宽度:
javascript
new TableCell({ children: [...] }) // 没有宽度!
❌ 使用百分比:
javascript
width: { size: 100, type: WidthType.PERCENTAGE } // 在Google Docs中出错
❌ 错误的底纹类型:
javascript
shading: { type: ShadingType.SOLID, fill: E0F2F1 } // 黑色背景!
❌ 列宽不匹配:
javascript
columnWidths: [2000, 7000] // = 9000,不是9360
✅ 正确:
javascript
new TableCell({
width: { size: 1872, type: WidthType.DXA },
shading: { type: ShadingType.CLEAR, fill: E0F2F1 },
margins: { top: 80, bottom: 80, left: 120, right: 120 },
borders: cellBorders,
children: [...]
})
表头颜色:
| 颜色 | 十六进制 | 用途 |
|-------|-----|-----
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 pretty-tables-1776289442 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 pretty-tables-1776289442 技能
skillhub install pretty-tables-1776289442
文件大小: 3.35 KB | 发布时间: 2026-4-17 15:48