1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| const fs = require('fs'); const path = require('path'); const { Document, Packer, Paragraph, TextRun, HeadingLevel, Table, TableRow, TableCell, WidthType } = require('docx');
const doc = new Document({ sections: [ { properties: { page: { size: { width: 11906, height: 16838 }, margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 }, }, }, children: [ new Paragraph({ text: '个人信息登记表', heading: HeadingLevel.HEADING_1, alignment: 'center', }), new Paragraph({ children: [ new TextRun('姓名:张三'), new TextRun({ text: '\n年龄:28', break: 1 }), ], }), new Paragraph(''), new Paragraph({ text: '技能清单', heading: HeadingLevel.HEADING_2, }), new Table({ width: { size: 100, type: WidthType.PERCENTAGE }, rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph('技能名称')] }), new TableCell({ children: [new Paragraph('熟练程度')] }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph('JavaScript')] }), new TableCell({ children: [new Paragraph('精通')] }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph('Node.js')] }), new TableCell({ children: [new Paragraph('熟练')] }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph('Vue')] }), new TableCell({ children: [new Paragraph('熟练')] }), ], }), ], }), new Paragraph(''), new Paragraph({ children: [ new TextRun({ text: '⚠️ 该用户为VIP会员,享有专属权益', bold: true, color: 'FF0000', }), ], }), ], }, ], });
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync(path.resolve(__dirname, '个人信息表_编程式.docx'), buffer); console.log('文档生成成功!'); });
|