1、数据写入excel export2excel
安装 export2excel
$ npm install -s export2excel
安装 xlxs
$ npm install -s xlxs
安装 file-saver
$ npm install -s file-saver
在src文件新建excel文件,百度网盘自提
链接:https://pan.baidu.com/s/1XpOdb7k1N9HftsIINO7Z_Q
提取码:9t3P
export2Excel() { var that = this; require.ensure([], () => { // 这里必须使用绝对路径,使用@/+存放export2Excel的路径 const { export_json_to_excel } = require("@/excel/export2Excel"); const multiHeader = ["", "", "", "", "", "备注", "", "", ""]; // 导出的excel的表头字段 const multiHeader2 = [ "名称", "名称", "标次", "检查", "挑时", "备注", "备注", "备注", "备注" ]; // 导出的excel的表头字段 const filterVal = [ "name", "name", "mark", "edit", "time", "info", "info", "rinfo", "_info" ]; // 对象属性,对应于multiHeader2 const list = that.excelData; //json数组对象,接口返回的数据 const data = that.formatJson(filterVal, list); //合并单元格 let merges = ["F2:I2"]; export_json_to_excel( multiHeader2, // multiHeader2: multiHeader2, data, "标图统计", merges ); // 导出的表格名称 }); },
formatJson(filterVal, jsonData) { return jsonData.map(v => filterVal.map(j => v[j]));
},
多级表头没实现、、、、单数数据很全,就算表格fixed也可以
2、安装XLSX直接下载
安装 npm install --save xlsx
export2Excel() { // 得自己处理表格fixed的情况 var ws1 = XLSX.utils.table_to_book(document.querySelector("#Table1")); //对应要导出的表格id /* get binary string as output */ var wbOut = XLSX.write(ws1, { bookType: "xlsx", bookSST: true, type: "array" }); try { FileSaver.saveAs( new Blob([wbOut], { type: "application/octet-stream" }), "demo.xlsx" ); } catch (e) { if (typeof console !== "undefined") console.log(e, wbOut); } return wbOut; },
页面显示的格式都能下载,但是如果表里有fixed字段,则会重复下载