直接上代码
第一步
cnpm install vue-json-excel
安装依赖包
第二步
main.js中注册使用
import JsonExcel from 'vue-json-excel
注册全局组件(也可注册局部组件)
Vue.component('downloadExcel', JsonExcel)
第三步
组件中使用
<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>
数据格式
"头部-名称": "name", //常规字段
"头部-电话": "phone.mobile", //支持嵌套属性
"头部-代码": {
field: "phone.landline",
//自定义回调函数
callback: value => {
return `处理数据 - ${value}`;
}
}
}
{
name: "name一",
city: "New York",
country: "United States",
birthdate: "1978-03-15",
phone: {
mobile: "1-541-754-3010",
landline: "(541) 754-3010"
}
},
{
name: "name二",
city: "Athens",
country: "Greece",
birthdate: "1987-11-23",
phone: {
mobile: "+1 855 275 5071",
landline: "(2741) 2621-244"
}
}
]
效果