<!DOCTYPE html> <html> <head> <title>table2xls</title> <meta charset="utf-8"> <style type="text/css"> body, table { font-size: 12px; } table { table-layout: fixed; empty-cells: show; border-collapse: collapse; margin: 0 auto; } td { height: 30px; } h1, h2, h3 { font-size: 12px; margin: 0; padding: 0; } .table { border: 1px solid #cad9ea; color: #666; } .table th { background-repeat: repeat-x; height: 30px; } .table td, .table th { border: 1px solid #cad9ea; padding: 0 1em 0; } .table tr.alter { background-color: #f5fafe; } div { margin-top: 2rem; } </style> </head> <body> <div> <table width="90%" class="table" id="table_outputXLS"> <tr> <th> 学号 </th> <th> 姓名 </th> <th> 平时 </th> <th> 期中 </th> <th> 实验(践)</th> <th> 其它 </th> <th> 期末 </th> <th> 总评 </th> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr class="alter"> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> </tr> </table> </div> <div style="margin:2rem auto;width: 6rem;"> <button type="button" id="btn_outputXLS">导出</button> </div> </body> </html> <script type="text/javascript"> //HTMLtable表格拷贝到EXCEL中(引用自:https://www.cnblogs.com/zhj-Acmen/p/7298936.html) var idTmr; // 释放资源计时器 — IE var fileName = "导出表格"; window.onload = function() { $$(‘btn_outputXLS‘).addEventListener("click", function() { outputXLS(‘table_outputXLS‘); }); } // 表格导出到EXCEL中 function outputXLS(tableid) { if ($$(tableid).rows.length > 2) { if (getExplorer() == ‘ie‘) { //AllAreaExcel(tableid); tableToXls_isIE(tableid); } else { tableToXls_notIE(tableid); } } else { alter("表内有内容才能导出!"); } } // table 导出为 xls - IE,需要降低浏览器安全级别,成功率低 function tableToXls_isIE(tableid) { var curTbl = $$(tableid); var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel var oWB = oXL.Workbooks.Add(); //获取workbook对象 var xlsheet = oWB.Worksheets(1); //激活当前sheet var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 sel.select; //全选TextRange中内容 sel.execCommand("Copy"); //复制TextRange中内容 xlsheet.Paste(); //粘贴到活动的EXCEL中 oXL.Visible = true; //设置excel可见属性 try { var fname = oXL.Application.GetSaveAsFilename(fileName + ".xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("导出错误:" + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); //xls.visible = false; oXL.Quit(); oXL = null; //结束excel进程,退出完成 idTmr = window.setInterval("Cleanup();", 1); } } // table 导出为 xls - 非IE function tableToXls_notIE(tableid) { let uri = ‘data:application/vnd.ms-excel;base64,‘; let 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>‘; let base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }; let format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }; // 导出 function outputHandler(name) { let table = $$(tableid); let ctx = { worksheet: name || ‘Worksheet1‘, table: table.innerHTML }; let a = document.createElement("a"); a.href = uri + base64(format(template, ctx)); a.download = fileName; a.click(); }; outputHandler(); } // 判断浏览器 function getExplorer() { var explorer = window.navigator.userAgent; //ie if (explorer.indexOf("MSIE") >= 0) { return ‘ie‘; } //firefox else if (explorer.indexOf("Firefox") >= 0) { return ‘Firefox‘; } //Chrome else if (explorer.indexOf("Chrome") >= 0) { return ‘Chrome‘; } //Opera else if (explorer.indexOf("Opera") >= 0) { return ‘Opera‘; } //Safari else if (explorer.indexOf("Safari") >= 0) { return ‘Safari‘; } } // 释放资源 - IE function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); // IE 的一个特有属性,用于释放内存的 } // 工具函数 - 通过 id 获取元素 function $$(id) { return document.getElementById(id); } </script>
copy大神的代码:https://www.cnblogs.com/nb08611033/p/8288042.html