docxTemplater——从word模板生成docx文件

官网文档:Get Started (Browser) | docxtemplater

官网在线演示:Demo of Docxtemplater with all modules active | docxtemplater

源码:https://github.com/open-xml-templating/docxtemplater

不仅可以处理word(免费),也可excel、html、ppt等等

优点:使用简单直观,可处理多种类型文件

缺点:开源支持功能较少,收费项目较多

1.在浏览器中使用(3步)

  • 根据需求定义word模板
  • 组织数据
  • 将数据填充到模板中

代码部分:

(1) 安装模块

docxtemplater、file-saver、pizzip、angular-expressions

(2) 编写

官方例子写法:Vuejs Docxtemplater Example - StackBlitz

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| import React, { Component } from 'react'; import Docxtemplater from 'docxtemplater'; import PizZip from 'pizzip'; import PizZipUtils from 'pizzip/utils/index.js'; import { saveAs } from 'file-saver'; import expressionParser from 'docxtemplater/expressions'; function loadFile(url, callback) { PizZipUtils.getBinaryContent(url, callback); } export const App = class App extends React.Component { render() { const generateDocument = () => { loadFile( 'https://docxtemplater.com/ang-example.docx', function (error, content) { if (error) { throw error; } const zip = new PizZip(content); const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true, parser: expressionParser, }); doc.render({ first_name: 'John', last_name: 'Doe', organization: { companyName: 'Foobar', }, phone: '0652455478', description: 'New Website', }); const out = doc.getZip().generate({ type: 'blob', mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', }); //Output the document using Data-URI saveAs(out, 'output.docx'); } ); }; return ( <div className="p-2"> <h1>Test docxtemplater</h1> <button onClick={generateDocument}>Generate document</button> <p>Click the button above to generate a document using ReactJS</p> <p> You can edit the data in your code in this example. In your app, the data would come from your database for example. </p> </div> ); } }; |

2.在node中使用

详见官网:Get Started (Node) | docxtemplater

相关推荐
百***35488 分钟前
JavaScript在Node.js中的集群部署
开发语言·javascript·node.js
光影少年10 分钟前
node.js和nest.js做智能体开发需要会哪些东西
开发语言·javascript·人工智能·node.js
华仔啊14 分钟前
图片标签用 img 还是 picture?很多人彻底弄混了!
前端·html
lichong95120 分钟前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端
南山安22 分钟前
栈(Stack):从“弹夹”到算法面试题的进阶之路
javascript·算法·面试
烟袅40 分钟前
作用域链 × 闭包:三段代码,看懂 JavaScript 的套娃人生
前端·javascript
San30.1 小时前
深入理解 JavaScript 异步编程:从 Ajax 到 Promise
开发语言·javascript·ajax·promise
风止何安啊1 小时前
收到字节的短信:Trae SOLO上线了?尝尝鲜,浅浅做个音乐播放器
前端·html·trae
抱琴_1 小时前
大屏性能优化终极方案:请求合并+智能缓存双剑合璧
前端·javascript
用户463989754321 小时前
Harmony os——长时任务(Continuous Task,ArkTS)
前端