下载
npm install vue-json-excel --save
使用
import JsonExcel from 'vue-json-excel'
// Vue.component('downloadExcel', JsonExcel) 全局导入 不推荐
<!--
* @Author: zhang_gen_yuan
* @Date: 2020-12-08 14:29:52
-->
<template>
<div>
<download-excel
class="export-excel-wrapper"
:data="json_data"
:fields="json_fields"
name="陈奎宁.xls"
>
<el-button type="primary" size="small">导出EXCEL</el-button>
</download-excel>
</div>
</template>
<script>
import JsonExcel from "vue-json-excel";
export default {
components: {
downloadExcel: JsonExcel,
},
data() {
return {
//定义表格 字段
json_fields: {
'姓名': "name",
'年龄': "age",
'城市': "city",
'爱好':'ai'
},
//table数据 需要和字段对应
json_data: [],
//解析excel utf-8 不然很可能导致乱码
json_meta: [
[
{
" key ": " charset ",
" value ": " utf- 8 ",
},
],
],
};
},
methods:{
// 模拟获取数据 赋值给 data
getTable(){
this.json_data = [
{
name:'陈奎宁',
age:18,
city:'郑州',
ai:'狗'
}
]
}
},
created(){
this.getTable();
}
};
</script>
<style>
</style>