JS 将table导出为Excel文档

            <table id="dataTable"></table>

            <button onclick="tableToExcel()">导出当前页列表</button>

<script> function tableToExcel() { var excelContent = $("#dataTable").html(); var excelFile = "<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'>"; excelFile += "<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>"; excelFile += "<body><table width='10%' border='1'>"; excelFile += excelContent.replace("height: 0px;", "height: 82px;"); excelFile += "</table></body>"; excelFile += "</html>"; var link = "data:application/vnd.ms-excel;base64," + base64(excelFile); var a = document.createElement("a"); a.download = $("#P_PlatformName option:selected").val() + timest() + ".xlsx"; a.href = link; a.click(); } function base64(content) { return window.btoa(unescape(encodeURIComponent(content))); } function timest() { var tmp = Date.parse(new Date()).toString(); tmp = tmp.substr(0, 10); return tmp; } </script>
原文地址:https://www.cnblogs.com/ZxtIsCnblogs/p/12786469.html