方式一 使用微软接口
var tabelStr = document.querySelector('.html-tabel').outerHTML;
var uri = 'data:application/vnd.ms-excel;base64,';
// 真正要导出(下载)的HTML模板
var exportTemplate = `<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>
${tabelStr}
</body>
</html>`;
之后添加a标签,赋值href链接
a.href = uri + base64(exportTemplate);
function base64(s) {
return window.btoa(unescape(encodeURIComponent(s)))
};
btoa: binary to ascii , 用于将ascii字符串
或二进制数据
转换成一个base64编码过的字符串
表示