一般用第三方包处理。常见的两个包: docxhtml-docx-js

docx

  1. const docx = require("docx@6.0.0")
  2. const doc = new Document({
  3. sections: [{
  4. properties: {},
  5. children: [
  6. new Paragraph({
  7. children: [
  8. new TextRun("Hello World"),
  9. new TextRun({
  10. text: "Foo Bar",
  11. bold: true,
  12. }),
  13. new TextRun({
  14. text: "\tGithub is the best",
  15. bold: true,
  16. }),
  17. ],
  18. }),
  19. ],
  20. }],
  21. });

react 中使用的 Demo:这里

html-docx-js

  1. var converted = htmlDocx.asBlob(content, {orientation: 'landscape', margins: {top: 720}});
  2. saveAs(converted, 'test.docx');