vue實(shí)現(xiàn)導(dǎo)出word文檔的示例代碼
vue 導(dǎo)出word文檔(包括圖片)
1.打開(kāi)終端,安裝依賴(lài)
-- 安裝 docxtemplater npm install docxtemplater pizzip --save -- 安裝 jszip-utils npm install jszip-utils --save -- 安裝 jszip npm install jszip --save -- 安裝 FileSaver npm install file-saver --save -- 安裝 angular-expressions npm install angular-expressions --save -- 安裝 image-size npm install image-size --save
2.創(chuàng)建exportFile.js文件(導(dǎo)出word方法)
文件存放目錄自定義
import PizZip from 'pizzip' import docxtemplater from 'docxtemplater' import JSZipUtils from 'jszip-utils' import { saveAs } from 'file-saver' /** * 將base64格式的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer * @param {Object} dataURL base64格式的數(shù)據(jù) */ function base64DataURLToArrayBuffer (dataURL) { const base64Regex = /^data:image\/(png|jpg|jpeg|svg|svg\+xml);base64,/; if (!base64Regex.test(dataURL)) { return false; } const stringBase64 = dataURL.replace(base64Regex, ""); let binaryString; if (typeof window !== "undefined") { binaryString = window.atob(stringBase64); } else { binaryString = new Buffer(stringBase64, "base64").toString("binary"); } const len = binaryString.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { const ascii = binaryString.charCodeAt(i); bytes[i] = ascii; } return bytes.buffer; } /** * 導(dǎo)出word,支持圖片 * @param {Object} tempDocxPath 模板文件路徑 * @param {Object} wordData 導(dǎo)出數(shù)據(jù) * @param {Object} fileName 導(dǎo)出文件名 * @param {Object} imgSize 自定義圖片尺寸 */ export const exportWord = (tempDocxPath, wordData, fileName, imgSize) => { // 這里要引入處理圖片的插件 var ImageModule = require('docxtemplater-image-module-free'); const expressions = require("angular-expressions"); // 讀取并獲得模板文件的二進(jìn)制內(nèi)容 JSZipUtils.getBinaryContent(tempDocxPath, function (error, content) { if (error) { throw error; } expressions.filters.size = function (input, width, height) { return { data: input, size: [width, height], }; }; // function angularParser (tag) { // const expr = expressions.compile(tag.replace(/'/g, "'")); // return { // get (scope) { // return expr(scope); // }, // }; // } // 圖片處理 let opts = {} opts = { // 圖像是否居中 centered: false }; opts.getImage = (chartId) => { // console.log(chartId);//base64數(shù)據(jù) // 將base64的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer return base64DataURLToArrayBuffer(chartId); } opts.getSize = function (img, tagValue, tagName) { // console.log(img);//ArrayBuffer數(shù)據(jù) // console.log(tagValue);//base64數(shù)據(jù) // console.log(tagName);//wordData對(duì)象的圖像屬性名 // 自定義指定圖像大小 if (imgSize.hasOwnProperty(tagName)){ return imgSize[tagName]; } else { return [600, 350]; } } // 創(chuàng)建一個(gè)PizZip實(shí)例,內(nèi)容為模板的內(nèi)容 let zip = new PizZip(content); // 創(chuàng)建并加載docxtemplater實(shí)例對(duì)象 let doc = new docxtemplater(); doc.attachModule(new ImageModule(opts)); doc.loadZip(zip); doc.setData(wordData); try { // 用模板變量的值替換所有模板變量 doc.render(); } catch (error) { // 拋出異常 let e = { message: error.message, name: error.name, stack: error.stack, properties: error.properties }; console.log(JSON.stringify({ error: e })); throw error; } // 生成一個(gè)代表docxtemplater對(duì)象的zip文件(不是一個(gè)真實(shí)的文件,而是在內(nèi)存中的表示) let out = doc.getZip().generate({ type: "blob", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }); // 將目標(biāo)文件對(duì)象保存為目標(biāo)類(lèi)型的文件,并命名 saveAs(out, fileName); }); }
3.組件調(diào)用
注意:引用文件的路徑是你創(chuàng)建文件的路徑
<template> <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol" > <a-form-model-item label="名稱(chēng)" prop="name"> <a-input-number v-model="form.name" style="width:100%;"/> </a-form-model-item> <a-form-model-item label="日期" prop="date"> <a-input v-model="form.date" /> </a-form-model-item> <a-form-model-item label="文件"> <a-input v-model="form.imgUrl" read-only/> <a-upload name="file" :showUploadList="false" :customRequest="customRequest"> <a-button type="primary" icon="upload">導(dǎo)入圖片</a-button> </a-upload> </a-form-model-item> <a-form-model-item label="操作"> <a-button type="primary" icon="export" @click="exportWordFile">導(dǎo)出word文檔</a-button> </a-form-model-item> </a-form-model> </template> <script> import {exportWord} from '@/assets/js/exportFile.js' export default { name: 'ExportFile', data () { return { labelCol: { span: 6 }, wrapperCol: { span: 16 }, form: {}, rules: { name: [ { required: true, message: '請(qǐng)輸入名稱(chēng)!', trigger: 'blur' }, ], date:[ { required: true, message: '請(qǐng)輸入日期!', trigger: 'blur' }, ], }, }; }, created (){}, methods: { customRequest (data){ //圖片必須轉(zhuǎn)成base64格式 var reader = new FileReader(); reader.readAsDataURL(data.file); reader.onload = () => { // console.log("file 轉(zhuǎn) base64結(jié)果:" + reader.result); this.form.imgUrl = reader.result; //imgUrl必須與模板文件里的參數(shù)名一致 }; reader.onerror = function (error) { console.log("Error: ", error); }; }, exportWordFile (){ let imgSize = { imgUrl:[65, 65], //控制導(dǎo)出的word圖片大小 }; exportWord("./static/test.docx", this.form, "demo.docx", imgSize); //參數(shù)1:模板文檔 //參數(shù)2:字段參數(shù) //參數(shù)3:輸出文檔 //參數(shù)4:圖片大小 } }, }; </script>
4.創(chuàng)建test.docx文檔模板
注:使用vue-cli2的時(shí)候,放在static目錄下;使用vue-cli3的時(shí)候,放在public目錄下。
模板參數(shù)名需與字段一致,普通字段:{字段名},圖片字段:{%字段名}
文檔內(nèi)容:
5.導(dǎo)出demo.docx
結(jié)果展示:
以上就是vue實(shí)現(xiàn)導(dǎo)出word文檔的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于vue導(dǎo)出word的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue?CompositionAPI中watch和watchEffect的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Vue?CompositionAPI中watch和watchEffect的區(qū)別,文中的示例代碼簡(jiǎn)潔易懂,希望對(duì)大家學(xué)習(xí)Vue有一定的幫助2023-06-06ElementUI時(shí)間選擇器限制選擇范圍disabledData的使用
本文主要介紹了ElementUI時(shí)間選擇器限制選擇范圍disabledData的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06vue實(shí)現(xiàn)登陸登出的實(shí)現(xiàn)示例
本篇文章主要介紹了vue實(shí)現(xiàn)登陸登出的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09axios請(qǐng)求的一些常見(jiàn)操作實(shí)戰(zhàn)指南
axios是一個(gè)輕量的HTTP客戶(hù)端,它基于XMLHttpRequest服務(wù)來(lái)執(zhí)行 HTTP請(qǐng)求,支持豐富的配置,支持Promise,支持瀏覽器端和 Node.js 端,下面這篇文章主要給大家介紹了關(guān)于axios請(qǐng)求的一些常見(jiàn)操作,需要的朋友可以參考下2022-09-09