var content = ‘‘;content += ‘<tr style="text-align:center">‘;
content += ‘<td>表头</td>‘ + ‘<td>表头</td>‘;
content += ‘</tr>‘;rows.forEach(function(row){ content += ‘<tr>‘; content += ‘<td>‘ + row.id + ‘</td>‘; content += ‘<td>‘ + (row.name) + ‘</td>‘; content += ‘</tr>‘;});
var uri=‘data:application/vnd.ms-excel;charset=utf-8,‘;var template = ‘<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>‘;var format=function(s,c){return s.replace(/{(\w+)}/g,function(m,p){return c[p];})}; var link = document.createElement("a");link.href = uri+encodeURIComponent(format(template,{worksheet:fileName||‘data‘,table:content})); link.style = "visibility:hidden";link.download = fileName + ".xls"; document.body.appendChild(link);link.click();document.body.removeChild(link); 主要思路就是构建一个表格,然后加到红色部分的代码里面去
时间: 2024-10-13 20:41:31