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

相关推荐
Larcher5 分钟前
JS 变量提升:代码没动,为什么执行顺序就变了?
前端·javascript·前端框架
yingyima5 分钟前
MySQL 事件调度器速查:核心语法与实战代码
前端
GISer_Jing6 分钟前
Claude Code多Agent架构深度剖析
前端·人工智能·架构·自动化
comphub7 分钟前
comp-hub:让你的 Vue 业务组件真正"活"起来
前端
AI砖家8 分钟前
Claude Code 跳过确认完全指南:让 AI 自己完成开发任务
前端·人工智能·python·ai编程·代码规范
KaMeidebaby25 分钟前
卡梅德生物技术快报|Pull Down 实验在 lncRNA - 蛋白互作机制研究中的应用实例解析
大数据·前端·架构·spark·新浪微博
锋行天下35 分钟前
让nginx网关扛下所有攻击
前端·后端·nginx
广州华水科技1 小时前
单北斗GNSS是什么?主要有哪些形变监测应用?
前端
就叫_这个吧1 小时前
JavaScript中常用事件示例展示附源码
开发语言·javascript·html
边界条件╝1 小时前
微前端进阶(二)
前端