纯前端导出xls表
/* tableData:表格数据 fieldLabels:表头字段 fieldKeys:表头字段对应的数据字段 fileName:导出文件名称 */ excelUtils(tableData, fieldLabels, fieldKeys, fileName) { let dataStr = fieldLabels.toString() + "\r\n"; tableData.forEach((item) => { fieldKeys.forEach((key) => { // 加引号是为了使换行符在单元格内正常显示 dataStr += `"${item[key]}"\t,`; }); dataStr += "\r\n"; }); const url = "data:text/xls;charset=utf-8,\ufeff" + encodeURIComponent(dataStr); const link = document.createElement("a"); link.href = url; link.download = fileName + ".xls"; link.style.display = "none"; document.body.appendChild(link); link.click(); document.body.removeChild(link); //释放标签 this.closeExport(); },