日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用docxtemplater模板语法,导出多个word文件,并生成zip压缩包

發(fā)布時間:2024/1/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用docxtemplater模板语法,导出多个word文件,并生成zip压缩包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 前言:
    • 一、插件準備:
    • 二、創(chuàng)建一個 download-zip.ts 文件,內(nèi)容為:
    • 三、頁面使用:
    • 四、word模板:
    • 五、結果如下:
    • 六、基本語法:

前言:

1、前端使用的是vue、element-ui 框架
2、el-table 多選列表,批量勾選數(shù)據(jù)后,需要每一條數(shù)據(jù)生成一個word文件,最終導出一個zip壓縮包

一、插件準備:

npm install --save docxtemplater pizzip jszip-utils jszip file-saver

如果想使用高級語法,可引入以下兩個插件, 可解析更多表達式:

npm install --save angular-expressions lodash

二、創(chuàng)建一個 download-zip.ts 文件,內(nèi)容為:

import docxtemplater from 'docxtemplater'; import PizZip from 'pizzip'; import JSZipUtils from 'jszip-utils' import { saveAs } from 'file-saver'; import JSZip from 'jszip';// 引入angular-expression 解析器 let expressions = require("angular-expressions"); let assign = require("lodash/assign"); expressions.filters.lower = function (input: any) {if (!input) return input;return input.toLowerCase(); }; function angularParser(tag: any) {tag = tag.replace(/^\.$/, "this").replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"');const expr = expressions.compile(tag);return {get: function (scope: any, context: any) {let obj = {};const scopeList = context.scopeList;const num = context.num;for (let i = 0, len = num + 1; i < len; i++) {obj = assign(obj, scopeList[i]);}return expr(scope, obj);},}; }// 導出方法 export const exportDocx = (tempDocxPath: string, list: any[], zipName: string) => {const promises: any[] = [];const zips = new JSZip();// 循環(huán)數(shù)據(jù),生成word文件list.forEach((element, index) => {let fileName = zipName + '(' + index + ')' + '.docx'; // word文件名稱let data = element;const promise = new Promise((resolver: any, reject) => {JSZipUtils.getBinaryContent(tempDocxPath, (error: any, content: any) => {if (error) {throw error;}let zip = new PizZip(content);let doc = new docxtemplater().loadZip(zip).setOptions({ parser: angularParser });doc.setData(data);try {doc.render();} catch (error: any) {throw error;}let out = doc.getZip().generate({type: 'blob',mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});// 添至zip集合中zips.file(fileName, out, { binary: true });resolver();})})promises.push(promise);})// 下載zip包Promise.all(promises).then(() => {zips.generateAsync({ type: 'blob' }).then((content: any) => {saveAs(content, zipName);})}) }

三、頁面使用:

import { exportDocx } from '@/utils/download-zip';/*** @description: 導出成績單 */ private exportPoliceInfoFn() {// 模擬數(shù)據(jù)const tempData = [{person: {number: '001',name: '張三',age: 20,sex: 0},results: [{subject: '語文',record: 95,teacher: '李白'},{subject: '數(shù)學',record: 130,teacher: '華羅庚'},{subject: '物理',record: 120,teacher: '錢學森'}]},{person: {number: '002',name: '李四',age: 22,sex: 1},results: [{subject: '語文',record: 115,teacher: '李白'},{subject: '數(shù)學',record: 140,teacher: '華羅庚'},{subject: '物理',record: 130,teacher: '錢學森'}]}];// 調(diào)用方法exportDocx('/doc/成績單.docx', tempData, '成績單'); }

四、word模板:

五、結果如下:


六、基本語法:

對象:

eg: let person = {name: '張三', age: 34};{person.name} {person.age} 或者 {#person} {name} {age} {/person}

數(shù)組循環(huán):

eg: let list = [{name: '張三', age: 12}, {name: '李四', age: 23}];{#list}{name} {age} {/list}

一維數(shù)組循環(huán):

eg: let array = ['one', 'two', 'three'];{#array}{.} {/array}

判斷:

{#isTrue} ... {/}

(完)

總結

以上是生活随笔為你收集整理的使用docxtemplater模板语法,导出多个word文件,并生成zip压缩包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。