很重要的两个插件
npm install --save xlsx file-saver
npm install -D script-loader
在src文件夹中新建文件夹(命名为excel)
新建文件夹后,在excel文件夹内新建两个js文件,分别命名为Blob.js、export2Excel.js,然后复制链接的代码分别到相关文件里
去百度盘里找找
<el-table
:data="tableData"
:border="true"
@selection-change="handleSelectionChange"
style="width: 100%"
>
首先不要忘了 @selection-change,然后是按钮
<el-button plain type="primary" @click="daochu">导出</el-button>
method方法
handleSelectionChange(val) { // 操作多选
this.multipleSelection = val // 多选的行会存入multipleSelection数组中
},
daochu() {
this.$confirm('确定下载列表文件?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.excelData = this.multipleSelection // multipleSelection是一个数组,存储表格中选择的行的数据。
this.export2Excel()
}).catch(() => {
})
},
// 数据写入excel
export2Excel() {
var that = this
require.ensure([], () => {
const { export_json_to_excel } = require('@/excel/export2Excel') // 这里必须使用绝对路径,使用@/+存放export2Excel的路径
const tHeader = ['一级分类', '二级分类', '三级分类', 'SKU编码', '调整前配送中心', '调整前库房编号', '调整后库房编号', '操作时间', '失败原因'] // 导出的表头名信息
const filterVal = ['itemFirstCateName', 'itemSecondCateName', 'itemThirdCateName', 'skuId', 'address', 'address', 'address', 'updateTimeString', 'changeResult'] // 导出的表头字段名,需要导出表格字段名
const list = that.excelData
const data = that.formatJson(filterVal, list)
console.log(data)
export_json_to_excel(tHeader, data, 'SKU库房属性失败明细表')// 导出的表格名称,根据需要自己命名
})
},
// 格式转换,直接复制即可
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
参考自https://blog.csdn.net/qq_26242601/article/details/91874261?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.baidujs&dist_request_id=1328655.10495.16158792048161585&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.baidujs