docxjs如何快速生成word文档?
|
admin
2025年6月23日 12:48
本文热度 112
|
docxjs是一款较为强大的docx格式文档处理的js库,基本可以处理我们遇到的所有有关于docx的文档问题。支持内置方式的word生成、解析、格式渲染。除此之外,我们还可以通过自定义方式直接通过xml方式渲染word包括但不限于字体样式、字体大小、段落样式、间距、颜色等一系列我们可能遇到的问题。 $npm install --save docx // 或者 $yarn add docx -D import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "docx";
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, }), ], }), ], }, ], });
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); });
Document是docxjs中封装word文档的顶级对象,是最后生成.docx的最终 封装类。您可以将所有的Paragraphs 添加到Document中,注意,一个.docx文档只有一个Document对象。 我们可以看下通过修改配置项实现改变document背景色的示例来了解下: const doc = new docx.Document({ background: { color: "C45911", }, });
每一个文档都是由多一个或者多个Sections构成的。一个Section是一组具有一组特定属性的Paragraphs ,这些属性用于定义将出现文本的页面。属性包括页面大小、页码、页面方向、页眉、边框和边距。例如,您可以有一个带有页眉和页脚的纵向部分,另一个没有页脚的横向部分,以及一个显示当前页码的页眉。 const doc =newDocument({ sections:[{ children:[ newParagraph({ children:[newTextRun("Hello World")], }), ], }]; });
Section Type 设置节类型决定了节的内容相对于前一节的放置方式。例如,有五种不同的类型:const doc = new Document({ sections: [{ properties: { type: SectionType.CONTINUOUS, } children: [ new Paragraph({ children: [new TextRun("Hello World")], }), ], }]; });
docx文件中OpenXML 中的所有内容(文本、图像、图表等)都按段落组织。Paragraphs 需要对Sections的理解。
构造创建
我们可以在创建paragraph对象时通过构造器直接初始化创建。 import { Paragraph } from "docx"; const paragraph = new Paragraph("Short hand Hello World");
子集嵌套创建
如果Paragraph中有多个子集,比如我一个段落里面既包含Text文本也有Image图片、Table之类元素的话,我们可以将它直接放在Paragraph的children属性里,children属性可传入一个数组对象。 const paragraph = new Paragraph({ children: [ new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World"), new SymbolRun("F071") ], });
Text单元素创建
如果Paragraph只有纯文本信息的话,我可以通过Paragraph的text属性进行创建。 const paragraph = new Paragraph({ text: "Short hand notation for adding text.", });
创建完paragraph对象后记得将其添加到对应的sections中: const doc = new Document({ sections: [{ children: [paragraph], }]; });
基本属性 属性 | 类型
| 强制的? | 可能的值 |
---|
text | string | 可选的 |
| heading | HeadingLevel | 可选的 | HEADING_1 ,
HEADING_2 ,
HEADING_3 ,
HEADING_4 ,
HEADING_5 ,
HEADING_6 ,
TITLE | border | IBorderOptions | 可选的 | top ,
bottom ,
left ,
right . 其中每一个都是属性对象都是 IBorderPropertyOptions 类型。单击此处查看示例 | spacing | ISpacingProperties | 可选的 | 请参阅下面的 ISpacingProperties | outlineLevel | number | 可选的 |
| alignment | AlignmentType | 可选的 |
| heading | HeadingLevel | 可选的 |
| bidirectional | boolean | 可选的 |
| thematicBreak | boolean | 可选的 |
| pageBreakBefore | boolean | 可选的 |
| contextualSpacing | boolean | 可选的 |
| indent | IIndentAttributesProperties | 可选的 |
| keepLines | boolean | 可选的 |
| keepNext | boolean | 可选的 |
| children | (TextRun or ImageRun or Hyperlink)[] | 可选的 |
| style | string | 可选的 |
| tabStop | { left?: ITabStopOptions; right?: ITabStopOptions; maxRight?: { leader: LeaderType; }; center?: ITabStopOptions } | 可选的 |
| bullet | { level: number } | 可选的 |
| numbering | { num: ConcreteNumbering; level: number; custom?: boolean } | 可选的 |
| widowControl | boolean | 可选的 |
| frame | IFrameOptions | 可选的
|
|
IBorderPropertyOptions属性配置
Paragraph的边框设置可以给boder设置top , bottom , left , right 四个属性对象,对象类型为IBorderPropertyOptions。
属性 | 类型
| 备注
|
---|
color | string | 必须 | space | number | 必须 | style | string | 必须 | size | number | 必须 |
ISpacingProperties
属性
| 类型
| 备注 | 可能的值
|
---|
after | number | 可选
|
| before | number | 可选
|
| line | number | 可选
|
| lineRule | LineRuleType | 可选
| AT_LEAST ,
EXACTLY ,
AUTO |
// 配置使用 const paragraph =new Paragraph({ text:"Hello World.", // text文本 heading: HeadingLevel.HEADING_1, // 标题 border: { top: { color: "auto", space: 1, style: "single", size: 6, }, bottom: { color: "auto", space: 1, style: "single", size: 6, }, }, // 边框配置 shading: { type: ShadingType.REVERSE_DIAGONAL_STRIPE, color: "00FFFF", fill: "FF0000", }, // 阴影配置 widowControl: true, // 允许第一行/最后一行显示在单独的页面上 spacing: { before: 200 }, // 边距配置,主要是配置段落格式的,如段前、段后、固定字高等 outlineLevel: 0, // 配置word文档的段落大纲类型 });
该文章在 2025/6/23 12:48:29 编辑过
|
|