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

相关推荐
未来之窗软件服务3 小时前
一体化系统(九)智慧社区综合报表——东方仙盟练气期
大数据·前端·仙盟创梦ide·东方仙盟·东方仙盟一体化
陈天伟教授6 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
信看7 小时前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
Tony Bai7 小时前
【API 设计之道】04 字段掩码模式:让前端决定后端返回什么
前端
爱吃大芒果7 小时前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
苏打水com7 小时前
第十四篇:Day40-42 前端架构设计入门——从“功能实现”到“架构思维”(对标职场“大型项目架构”需求)
前端·架构
king王一帅7 小时前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
苏打水com8 小时前
第十八篇:Day52-54 前端跨端开发进阶——从“多端适配”到“跨端统一”(对标职场“全栈化”需求)
前端
Bigger8 小时前
后端拒写接口?前端硬核自救:纯前端实现静态资源下载全链路解析
前端·浏览器·vite