vue導(dǎo)出word純前端的實(shí)現(xiàn)方式
vue導(dǎo)出word純前端實(shí)現(xiàn)
最近項(xiàng)目有個(gè)需求導(dǎo)出word,純前端實(shí)現(xiàn),查了查資料,用docxtemplater簡(jiǎn)直不要太簡(jiǎn)單。
直接把官網(wǎng)例子拿過來就可以了。!??!
首先,新建一個(gè)docx文件,把模板先寫好。
注意??!如果數(shù)據(jù)結(jié)構(gòu)中存在數(shù)組。 用{#xxx}{/xxx} 包裹。
數(shù)據(jù)結(jié)構(gòu)示例:
wordData: {
name: '導(dǎo)出word',
nameList: [{
name: "張三",
age: 16,
hobby: ['吃飯', '睡覺', '打豆豆']
},
{
name: "李四",
age: 19,
hobby: ['抽煙', '喝酒', '打麻將']
},
]
},

模板寫好之后放入項(xiàng)目中。
然后導(dǎo)入需要的包,
npm i docxtemplater pizzip file-saver --save
然后在需要的模塊內(nèi)引入
import 'docxtemplater/build/docxtemplater.js' import 'pizzip/dist/pizzip.js' import 'pizzip/dist/pizzip-utils.js' import 'file-saver'
methods: {
// 導(dǎo)出word
loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
},
generate() {
var that = this;
this.loadFile("../../static/word.docx", function (error, content) {
if (error) {
throw error
};
var zip = new PizZip(content);
var doc = new window.docxtemplater().loadZip(zip)
doc.setData({
...that.wordData
});
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render()
} catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
}
console.log(JSON.stringify({
error: e
}));
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
throw error;
}
var out = doc.getZip().generate({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}) //Output the document using Data-URI
saveAs(out, "output.docx")
})
},
}
到此就完事了。
完整代碼,安裝完包之后直接運(yùn)行就好。記得放入word模板!??!

<template> ? <div> ? ? <button @click="generate">導(dǎo)出</button> ? </div> </template>
<script>
? import 'docxtemplater/build/docxtemplater.js'
? import 'pizzip/dist/pizzip.js'
? import 'pizzip/dist/pizzip-utils.js'
? import 'file-saver'
? export default {
? ? data() {
? ? ? return {
? ? ? ? wordData: {
? ? ? ? ? name: '導(dǎo)出word',
? ? ? ? ? nameList: [{
? ? ? ? ? ? ? name: "張三",
? ? ? ? ? ? ? age: 16,
? ? ? ? ? ? ? hobby: ['吃飯', '睡覺', '打豆豆']
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? name: "李四",
? ? ? ? ? ? ? age: 19,
? ? ? ? ? ? ? hobby: ['抽煙', '喝酒', '打麻將']
? ? ? ? ? ? },
? ? ? ? ? ]
? ? ? ? },
? ? ? }
? ? },
? ? methods: {
? ? ? // 導(dǎo)出word
? ? ? loadFile(url, callback) {
? ? ? ? PizZipUtils.getBinaryContent(url, callback);
? ? ? },
? ? ? generate() {
? ? ? ? var that = this;
? ? ? ? this.loadFile("../../static/word.docx", function (error, content) {
? ? ? ? ? if (error) {
? ? ? ? ? ? throw error
? ? ? ? ? };
? ? ? ? ? var zip = new PizZip(content);
? ? ? ? ? var doc = new window.docxtemplater().loadZip(zip)
? ? ? ? ? doc.setData({
? ? ? ? ? ? ...that.wordData
? ? ? ? ? });
? ? ? ? ? try {
? ? ? ? ? ? // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
? ? ? ? ? ? doc.render()
? ? ? ? ? } catch (error) {
? ? ? ? ? ? var e = {
? ? ? ? ? ? ? message: error.message,
? ? ? ? ? ? ? name: error.name,
? ? ? ? ? ? ? stack: error.stack,
? ? ? ? ? ? ? properties: error.properties,
? ? ? ? ? ? }
? ? ? ? ? ? console.log(JSON.stringify({
? ? ? ? ? ? ? error: e
? ? ? ? ? ? }));
? ? ? ? ? ? // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
? ? ? ? ? ? throw error;
? ? ? ? ? }
? ? ? ? ? var out = doc.getZip().generate({
? ? ? ? ? ? type: "blob",
? ? ? ? ? ? mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
? ? ? ? ? }) //Output the document using Data-URI
? ? ? ? ? saveAs(out, "output.docx")
? ? ? ? })
? ? ? },
? ? }
? }
</script>
<style scoped>
</style>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue緩存的keepalive頁(yè)面刷新數(shù)據(jù)的方法
這篇文章主要介紹了vue緩存的keepalive頁(yè)面刷新數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
vue cli實(shí)現(xiàn)項(xiàng)目登陸頁(yè)面流程詳解
CLI是一個(gè)全局安裝的npm包,提供了終端里的vue命令。它可以通過vue create快速搭建一個(gè)新項(xiàng)目,或者直接通過vue serve構(gòu)建新想法的原型。你也可以通過vue ui通過一套圖形化界面管理你的所有項(xiàng)目2022-10-10
基于Vue3實(shí)現(xiàn)列表虛擬滾動(dòng)效果
這篇文章主要為大家介紹了如何利用Vue3實(shí)現(xiàn)列表虛擬滾動(dòng)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定價(jià)值,需要的可以參考一下2022-04-04
vue系列之requireJs中引入vue-router的方法
這篇文章主要介紹了vue系列之requireJs中引入vue-router的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Vue 實(shí)現(xiàn)登錄界面驗(yàn)證碼功能
本文通過實(shí)例代碼給大家介紹了Vue 實(shí)現(xiàn)登錄界面 驗(yàn)證碼功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
解決vue3.0運(yùn)行項(xiàng)目warning Insert `·` prettier/pret
這篇文章主要介紹了解決vue3.0運(yùn)行項(xiàng)目warning Insert `·` prettier/prettier問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式
在看element-ui的源碼的時(shí)候,注意到源碼里面有很多地方使用provide和inject的屬性,本文主要介紹了vue2實(shí)現(xiàn)provide inject傳遞響應(yīng)式,分享給大家,感興趣的可以了解一下2021-05-05

