// 输出base64编码 const base64 = s => window.btoa(unescape(encodeURIComponent(s))); const tableToExcel = async (jsonData, fileName) => { return new Promise(resolve => { try { let str = '' Object.keys(jsonData[0]).forEach(name => { const tdStr = `${name}` str += tdStr; }) str += '' for (let i = 0; i < jsonData.length; i++) { str += ''; for (const key in jsonData[i]) { // 增加\t为了不让表格显示科学计数法或者其他格式 str += `${jsonData[i][key] + '\t'}`; } str += ''; } const currentTime = new Date() const fileSuffix = currentTime.valueOf() const name = fileName + '质量分' + fileSuffix // Worksheet名 const worksheet = '用户质量分'; const uri = 'data:application/vnd.ms-excel;base64,'; // 下载的表格模板数据 const template = ` ${str}
`; // 下载模板 // window.location.href = uri + base64(template); const downloadLink = document.createElement("a"); downloadLink.href = uri + base64(template); downloadLink.download = name + '.xls'; downloadLink.target = '_blank'; document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); resolve(true) } catch (r) { console.log(r, 'html excel') resolve(false) } }) }; export const exportExcelFunc = async (tableData) => { return new Promise(async (resolve) => { try { await tableToExcel(tableData) resolve(true) } catch (r) { console.log(r, 'exportExcelFunc') resolve(false) } }) }