一般用第三方包处理。常见的两个包: docx 和 html-docx-js。
docx
const docx = require("docx@6.0.0")const doc = new Document({sections: [{properties: {},children: [new Paragraph({children: [new TextRun("Hello World"),new TextRun({text: "Foo Bar",bold: true,}),new TextRun({text: "\tGithub is the best",bold: true,}),],}),],}],});
react 中使用的 Demo:这里。
html-docx-js
var converted = htmlDocx.asBlob(content, {orientation: 'landscape', margins: {top: 720}});saveAs(converted, 'test.docx');
