1.首先我们需要安装3个依赖,file-saver、xlsx和script-loader。
2.在src目录下新建vendor文件夹,里面放入Blob.js和Export2Excel.js两个JS文件(文件可以去网上下载)
3.在main.js中引入两个包`
import Blob from './vendor/Blob'
import Export2Excel from './vendor/Export2Excel.js'`
4.具体代码
exportToExcel() {//导出数据
require.ensure([], () => {
const { export_json_to_excel } = require("../../vendor/Export2Excel");
const tHeader = [
"标题",
"标题",
"标题",
"标题",
"标题",
"标题",
"标题",
];
const filterVal = [
"cash_no",
"company_name",
"credit_code",
"company_public_bank",
"form_state",
"apply_form_state",
"create_time",
];
const list = this.activities;
const data = this.formatJson(filterVal, list);
export_json_to_excel(tHeader, data, "兑换详情列表");
});
},
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]));
},