简要介绍
docx是一款可以通过JavaScript生成.docx文件的组件。它在github上有1.4k颗星。目前支持与主流的React、Vue、Angular框架的集成。它的官网文档非常详细,值得称赞。小伙伴以后没有office软件,也可以生成word文件了,是不是非常方便。目前最新版本:v5.4.1。
Github地址
https://github.com/dolanmiu/docx
安装
npm install --save docx
引用
const docx = require("docx");
或者
import * as docx from "docx";
效果
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
// Create document
const doc = new Document();
// Documents contain sections, you can have multiple sections per document, go here to learn more about sections
// This simple example will only contain one section
doc.addSection({
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,
}),
],
}),
],
});
// Used to export the file into a .docx file
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
// Done! A file called 'My Document.docx' will be in your file system.