导入js文件 tado.js :
function tableToExcel(tableID, sheetName, fileName) {
const uri = 'data:application/vnd.ms-excel;base64,';
const template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" '+
'xmlns:x="urn:schemas-microsoft-com:office:excel">'+
'<head><!--[if gte mso 9]><xml>'+
'<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'+
'<x:Name>'+sheetName+'</x:Name><x:WorksheetOptions>'+
'<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>'+
'</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->'+
'</head><body><table>{table}</table></body></html>';
const table = document.getElementById(tableID).outerHTML;
const base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))); };
const link = document.createElement('a');
link.href = uri + base64(template.replace('{table}', table));
link.download = fileName || 'download.xls';
link.click();
}
然后就可以在js里面使用函数 tableToExcel(tableID, sheetName, fileName)调用了
第一个参数为表格id 第二个为保存的sheet名 随便取 第三个为保存xls的文件名随便取
have fun