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

相关推荐
时光追逐者14 分钟前
在 Blazor 中使用 Chart.js 快速创建数据可视化图表
开发语言·javascript·信息可视化·c#·.net·blazor
无奈何杨16 分钟前
扣子coze的AI工作流搭建技术,开源项目FlowGram流程搭建引擎
前端
ElasticPDF-新国产PDF编辑器26 分钟前
Angular 项目 PDF 批注插件库在线版 API 示例教程
前端·pdf·angular.js
hz.ts30 分钟前
Angular 国际化
javascript·ecmascript·angular.js
6武730 分钟前
Vue 数据传递流程图指南
前端·javascript·vue.js
夏天想41 分钟前
vant4+vue3上传一个pdf文件并实现pdf的预览。使用插件pdf.js
开发语言·javascript·pdf·vant
....4921 小时前
antvX6节点全选后鼠标通过拖拉调整视图的展示位置
javascript·计算机外设·数据中台·antvx6
jakeswang1 小时前
查询条件与查询数据的ajax拼装
前端·ajax
samuel9181 小时前
axios取消重复请求
前端·javascript·vue.js