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

相关推荐
linux_cfan14 分钟前
16 · `custom-media-element`:属性拦截与转发
前端·javascript·音视频
右耳朵猫AI24 分钟前
Web前端周刊2026W38 | React 19.3 发布、StyleX 深潜、jsdom 30.1 提速
前端·javascript·react.js·typescript·node.js
落魄大学生之流水线上谋生计26 分钟前
GreenLife Carbon · OpenHarmony 智慧低碳生态平台
javascript
人工智能培训1 小时前
大语言模型:从语言理解到通用智能的跃迁
linux·服务器·前端·人工智能
一拳不是超人1 小时前
做独立开发三个月,我的第一个产品终于有了 1000 个用户
前端·程序员
Amos_Web1 小时前
Rspack 源码解析(十):Hash 与 Asset 生成
前端·rust·源码阅读
福兮说1 小时前
用 IndexedDB 存用户的文件,我踩过的五个坑
前端·javascript
闪耀之光M781 小时前
前端依赖自动导入:unplugin-auto-import
前端
Ricon组态薄荷糖2 小时前
Ricon组态系统:工业组件开发指南与实践
前端·后端·物联网
flash俊杰2 小时前
Electron 桌面应用的进程模型:为什么 fork Next.js standalone
前端