前端Vue.js實(shí)現(xiàn)json數(shù)據(jù)導(dǎo)出到doc
前言:
先說(shuō)下需求:如何批量導(dǎo)出表格中勾選的數(shù)據(jù),導(dǎo)出格式為word,并具有一定的格式!
開(kāi)發(fā)環(huán)境:vue3、node v14.15.0
組件選擇
因?yàn)閷?dǎo)出word需要保留一定的格式,所以這里選擇docxtemplater,利用定制導(dǎo)出模板的方式限制數(shù)據(jù)的導(dǎo)出格式。
實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的導(dǎo)出
安裝(后續(xù)的demo依賴(lài)相同)
yarn add docxtemplater pizzip file-saver
用法:
onst PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const fs = require("fs");
const path = require("path");
// Load the docx file as binary content
const content = fs.readFileSync(
path.resolve(__dirname, "input.docx"),
"binary"
);
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true,
});
// Render the document (Replace {first_name} by John, {last_name} by Doe, ...)
doc.render({
first_name: "John",
last_name: "Doe",
phone: "0652455478",
description: "New Website",
});
const buf = doc.getZip().generate({
type: "nodebuffer",
// compression: DEFLATE adds a compression step.
// For a 50MB output document, expect 500ms additional CPU time
compression: "DEFLATE",
});單條數(shù)據(jù)導(dǎo)出到word
配置導(dǎo)出模板,并上傳CDN,模板格式如下:

導(dǎo)出的數(shù)據(jù)結(jié)構(gòu):
{
"title": "第一篇",
"gradeId": "三年級(jí)上",
"genreDesc": "議論文",
"subjectMatterInfo": "敘事",
"content": "\t一個(gè)晴朗的星期天,爸爸和媽媽帶我到北海公園去玩兒。那天秋高氣爽,太陽(yáng)在藍(lán)",
"remark": "點(diǎn)評(píng)1"
}導(dǎo)出wrod截圖

具體實(shí)現(xiàn):
import Docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import PizZipUtils from 'pizzip/utils/index.js';
import {saveAs} from 'file-saver';
function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
}
export const renderDoc = data => {
loadFile('你上傳導(dǎo)出模板的CDN鏈接.docx', (error, content) => {
if (error) {
throw error;
}
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {linebreaks: true});
doc.setData(data);
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
}
catch (error) {
// The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
function replaceErrors(key, value) {
if (value instanceof Error) {
return Object.getOwnPropertyNames(value).reduce((error, key) => {
error[key] = value[key];
return error;
}, {});
}
return value;
}
console.log(JSON.stringify({error}, replaceErrors));
if (error.properties && error.properties.errors instanceof Array) {
const errorMessages = error.properties.errors
.map(error => error.properties.explanation)
.join('\n');
console.log('errorMessages', errorMessages);
// errorMessages is a humanly readable message looking like this :
// 'The tag beginning with "foobar" is unopened'
}
throw error;
}
const out = doc.getZip().generate({
type: 'blob',
mimeType:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
// Output the document using Data-URI
saveAs(out, '導(dǎo)出word.docx');
});
};批量數(shù)據(jù)導(dǎo)出到wrod
配置導(dǎo)出模板,并上傳CDN,模板格式如下:
#articles為循環(huán)開(kāi)始,/為循環(huán)結(jié)束,中間是循環(huán)體

導(dǎo)出的數(shù)據(jù)結(jié)構(gòu):
{
"articles": [
{
"title": "第一篇",
"gradeId": "三年級(jí)上",
"genreDesc": "議論文",
"subjectMatterInfo": "敘事",
"content": "\t一個(gè)晴朗的星期天,爸爸和媽媽帶我到北海公園去玩兒。那天秋高氣爽,太陽(yáng)在藍(lán)",
"remark": "點(diǎn)評(píng)1"
},
{
"title": "第二篇",
"gradeId": "三年級(jí)下",
"genreDesc": "記敘文",
"subjectMatterInfo": "寫(xiě)人",
"content": "\t一個(gè)晴朗的星期天,爸爸和媽媽帶我到北海公園去玩兒媽帶我到北海公園去玩兒。那天秋高氣爽,太陽(yáng)在藍(lán)。\n問(wèn)女何所思,問(wèn)女何所憶。女亦無(wú)所思,女亦無(wú)所憶。昨夜見(jiàn)軍帖,可汗大點(diǎn)兵,軍書(shū)十二卷,卷卷有爺名。阿爺無(wú)大兒,木蘭無(wú)長(zhǎng)兄,愿為市鞍馬,從此替爺征。問(wèn)女何所思,問(wèn)女何所憶。女亦無(wú)所思,女亦無(wú)所憶。昨夜見(jiàn)軍帖,可汗大點(diǎn)兵,軍書(shū)十二卷,卷卷有爺名。阿爺無(wú)大兒,木蘭無(wú)長(zhǎng)兄,愿為市鞍馬,從此替爺征。",
"remark": "點(diǎn)評(píng)2"
}
]
}導(dǎo)出wrod截圖:

具體實(shí)現(xiàn):
import Docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import PizZipUtils from 'pizzip/utils/index.js';
import {saveAs} from 'file-saver';
function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
}
export const renderDoc = data => {
loadFile('你上傳導(dǎo)出模板的CDN鏈接.docx', (error, content) => {
if (error) {
throw error;
}
const zip = new PizZip(content);
// paragraphLoop段落循環(huán)
const doc = new Docxtemplater(zip, {paragraphLoop: true, linebreaks: true});
doc.setData(data);
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
}
catch (error) {
// The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
function replaceErrors(key, value) {
if (value instanceof Error) {
return Object.getOwnPropertyNames(value).reduce((error, key) => {
error[key] = value[key];
return error;
}, {});
}
return value;
}
console.log(JSON.stringify({error}, replaceErrors));
if (error.properties && error.properties.errors instanceof Array) {
const errorMessages = error.properties.errors
.map(error => error.properties.explanation)
.join('\n');
console.log('errorMessages', errorMessages);
// errorMessages is a humanly readable message looking like this :
// 'The tag beginning with "foobar" is unopened'
}
throw error;
}
const out = doc.getZip().generate({
type: 'blob',
mimeType:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
// Output the document using Data-URI
saveAs(out, '導(dǎo)出word.docx');
});
};到此這篇關(guān)于前端Vue.js實(shí)現(xiàn)json數(shù)據(jù)導(dǎo)出到doc的文章就介紹到這了,更多相關(guān)Vue.js json數(shù)據(jù)導(dǎo)出doc內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題
今天小編就為大家分享一篇解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Vue實(shí)現(xiàn)移動(dòng)端左右滑動(dòng)效果的方法
最近得到一個(gè)新需求,需要在Vue項(xiàng)目的移動(dòng)端頁(yè)面上加上左右滑動(dòng)效果,經(jīng)過(guò)一番折騰,最終決定四月vue-touch。下面小編把實(shí)現(xiàn)代碼分享給大家,感興趣的朋友一起看看吧2018-11-11
解決vue3+vite配置unplugin-vue-component找不到Vant組件
這篇文章主要為大家介紹了vue3+vite配置unplugin-vue-component找不到Vant組件問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
vue 1.0 結(jié)合animate.css定義動(dòng)畫(huà)效果
本文分步驟給大家介紹了Vue 1.0自定義動(dòng)畫(huà)效果,vue1.0代碼結(jié)合animate.css定義動(dòng)畫(huà),頁(yè)面一定要引入animate.cdd,具體實(shí)例代碼大家參考下本文2018-07-07
詳解基于vue-cli3快速發(fā)布一個(gè)fullpage組件
這篇文章主要介紹了詳解基于vue-cli3快速發(fā)布一個(gè)fullpage組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
淺析vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for
這篇文章主要介紹了vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for,包括v-for遍歷數(shù)組,v-for遍歷json對(duì)象,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04
vue中關(guān)于router.beforeEach()的用法
這篇文章主要介紹了vue中關(guān)于router.beforeEach()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue響應(yīng)式原理模擬實(shí)現(xiàn)原理探究
這篇文章主要介紹了Vue響應(yīng)式原理,響應(yīng)式就是當(dāng)對(duì)象本身(對(duì)象的增刪值)或者對(duì)象屬性(重新賦值)發(fā)生了改變的時(shí)候,就會(huì)運(yùn)行一些函數(shù),最常見(jiàn)的示render函數(shù)2022-09-09

