1.将js粘贴到项目
2.设置table标签 id
3.定义按钮,调用方法即可
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8" /> <title>导出Excel</title> </head> <body> <button type="button" onclick="method5('tableExcel')">导出Excel</button> <table border="6" frame="void" align="center" width="650px" height="100px" cellspacing="0px" cellpadding="10px" id="tableExcel"> <tbody> <tr height="50px" align="center"> <th width="110px"></th> <th><font color="blue">星期一</font></th> <th><font color="blue">星期二</font></th> <th><font color="blue">星期三</font></th> <th><font color="blue">星期四</font></th> <th><font color="blue">星期五</font></th> </tr> <tr height="40px" align="center"> <td></td> <td colspan="5" color="red"><font color="red"><b>上午</b></font></td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>一</b></font></td> <td>数学</td> <td>英语</td> <td>语文</td> <td>文体活动</td> <td>数学</td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>二</b></font></td> <td>语文</td> <td>美术</td> <td>语文</td> <td>语文</td> <td>语文</td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>三</b></font></td> <td>体育</td> <td>文体活动</td> <td>文体活动</td> <td>品德与生活</td> <td>英语</td> </tr> <tr height="40px" align="center"> <td></td> <td colspan="5"><font color="red"><b>下午</b></font></td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>四</b></font></td> <td>品德与生活</td> <td>语文</td> <td>数学</td> <td>体育</td> <td>美术</td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>五</b></font></td> <td>文体活动</td> <td>体育</td> <td>专题</td> <td>语文</td> <td>语文</td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>六</b></font></td> <td>兴趣</td> <td>读书活动</td> <td>音乐</td> <td>音乐</td> <td>数学</td> </tr> <tr height="40px" align="center"> <td><font color="blue"><b>七</b></font></td> <td>班队活动</td> <td>课后托管</td> <td>课后托管</td> <td>课后托管</td> <td>课后托管</td> </tr> </tbody> </table> <script> //导出excel var idTmr; 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'; } } function method5(tableid) { if(getExplorer()=='ie') { var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var xlsheet = oWB.Worksheets(1); var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); sel.select(); sel.execCommand("Copy"); xlsheet.Paste(); oXL.Visible = true; try { var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); oXL.Quit(); oXL = null; idTmr = window.setInterval("Cleanup();", 1); } } else { tableToExcel(tableid) } } function Cleanup() { window.clearInterval(idTmr); // CollectGarbage(); } var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html><head><meta charset="UTF-8"></head><body><table border="1px">{table}</table></body></html>', base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }, format = function(s, c) { return s.replace(/{(w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })(); </script> </body> </html>