Create Word documents with properly formatted tables using docx npm library. Tables work consistently across Word and Google Docs. Use when creating DOCX files with tables, especially itineraries, schedules, or data tables.
创建可在任何地方正常使用的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
// ❌ 错误
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({
margins: cellMargins,
children: [...]
})
对于1英寸页边距:9360 DXA
javascript
columnWidths: [1872, 7488] // = 9360 ✓
columnWidths: [3120, 3120, 3120] // = 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;
const COL1 = 1872;
const COL2 = 7488;
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 };
const doc = new Document({
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } }
},
children: [
new Table({
layout: TableLayoutType.FIXED,
width: { size: TOTAL_WIDTH, type: WidthType.DXA },
columnWidths: [COL1, COL2],
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: Header, bold: true, color: FFFFFF })],
alignment: AlignmentType.CENTER
})],
width: { size: TOTAL_WIDTH, type: WidthType.DXA },
columnSpan: 2,
shading: { type: ShadingType.CLEAR, fill: 1565C0 },
borders: cellBorders,
margins: cellMargins
})
]
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph(Col 1)],
width: { size: COL1, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
}),
new TableCell({
children: [new Paragraph(Col 2)],
width: { size: COL2, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
})
]
})
]
})
]
}]
});
Packer.toBuffer(doc).then(buffer => {
fs.writeFileSync(output.docx, buffer);
});
对于带1英寸页边距的US Letter纸张 = 9360 DXA:
| 布局 | 列宽 |
|---|---|
| 2列(20/80) | [1872, 7488] |
| 2列(25/75) |
Google Docs中表格变窄?
单元格背景变黑?
文字紧贴边框?
列宽不均匀?
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 docx-tables-1776351602 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 docx-tables-1776351602 技能
skillhub install docx-tables-1776351602
文件大小: 2.18 KB | 发布时间: 2026-4-17 14:40