有趣生活

当前位置:首页>科技>javascript的文件结构JavaScript可以生成word文件的类库docx.js

javascript的文件结构JavaScript可以生成word文件的类库docx.js

发布时间:2026-07-21阅读(1)

导读简要介绍docx是一款可以通过JavaScript生成.docx文件的组件。它在github上有1.4k颗星。目前支持与主流的React、Vue、Angul....简要介绍

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.

TAGS标签:  javascript  文件  结构  可以  的文件结构JavaS

Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图