在開發(fā)的時(shí)候,會(huì)經(jīng)常用的導(dǎo)出excel表格功能,剛好自己開發(fā)有遇到佛致,就記錄一下
一、安裝vue-json-excel
npm install vue-json-excel -S
二辙谜、main.js中引入
import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)
三、在代碼中使用
<template>
<download-excel
class="export-excel-wrapper"
:data="DetailsForm"
:fields="json_fields"
:header="title"
name="需要導(dǎo)出的表格名字.xls"
>
<!-- 上面可以自定義自己的樣式感昼,還可以引用其他組件button -->
<el-button type="success">導(dǎo)出</el-button>
</download-excel>
</template>
四装哆、數(shù)據(jù)
DetailsForm:需要導(dǎo)出的數(shù)據(jù)
title:表格標(biāo)題
json_fields:里面的屬性是excel表每一列的title,用多個(gè)詞組組成的屬性名(中間有空格的)要加雙引號(hào); 指定接口的json內(nèi)某些數(shù)據(jù)下載,若不指定,默認(rèn)導(dǎo)出全部數(shù)據(jù)中心全部字段
<script>
title: "xx公司表格",
json_fields: {
"排查日期":'date',
"整改隱患內(nèi)容":'details',
"整改措施":'measure',
"整改時(shí)限":'timeLimit',
"應(yīng)急措施和預(yù)案":'plan',
"整改責(zé)任人":'personInCharge',
"填表人":'preparer',
"整改資金":'fund',
"整改完成情況":'complete',
"備注":'remark',
},
DetailsForm: [
{
date: "2022-3-10",
details: "卸油區(qū)過路燈損壞",
measure: "更換燈泡",
timeLimit: "2022-3-21",
plan: "先使用充電燈代替,貼好安全提醒告示",
personInCharge: "王xx",
preparer: "王xx",
fund: "20元",
complete: "已完成整改",
remark: "重新更換了燈泡",
},
],
</script>
image.png