import Vue from 'vue' import XLSX from 'xlsx' /** * 导出数据报表xlsx文件 * 已注入所有Vue实例, * template模板里调用 $$outputXlsxFile * 组件方法里调用 this.$outputXlsxFile * 例:this.$OutFile([['字段1', '字段2'], [1, 2]], [{wch: 10}, {wch: 50}], '测试导出') 得到 测试导出.xlsx 文件 * 第一个参数是导出的数组对象,第二个参数是设置字段宽度,第三个参数是文件名 */ const outputXlsxFile = (data, wscols, xlsxName) => { /* convert state to workbook */ const ws = XLSX.utils.aoa_to_sheet(data) ws['!cols'] = wscols const wb = XLSX.utils.book_new() XLSX.utils.book_append_sheet(wb, ws, xlsxName) /* generate file and send to client */ XLSX.writeFile(wb, xlsxName + ".xlsx") } Vue.prototype.$OutFile= outputXlsxFile
vue使用js-xlsx插件 导出excel表格