js页面上的excel导出
生活随笔
收集整理的這篇文章主要介紹了
js页面上的excel导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
js頁面上的excel導出
這里的代碼是網上轉別人的感覺還不錯,可以試試。 <html> <head> <script type="text/javascript" language="javascript">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';}//Chromeelse if(explorer.indexOf("Chrome") >= 0){return 'Chrome';}//Operaelse if(explorer.indexOf("Opera") >= 0){return 'Opera';}//Safarielse if(explorer.indexOf("Safari") >= 0){return 'Safari';}}function method1(tableid) {//整個表格拷貝到EXCEL中if(getExplorer()=='ie'){var curTbl = document.getElementById(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("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");} catch (e) {print("Nested catch caught " + e);} finally {oWB.SaveAs(fname);oWB.Close(savechanges = false);//xls.visible = false;oXL.Quit();oXL = null;//結束excel進程,退出完成//window.setInterval("Cleanup();",1);idTmr = window.setInterval("Cleanup();", 1);}}else{tableToExcel('ta')}}function Cleanup() {window.clearInterval(idTmr);CollectGarbage();}var tableToExcel = (function() {var uri = 'data:application/vnd.ms-excel;base64,',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>',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></head> <body> <table id="ta"><tr><td>1</td><td>admin</td><td>23</td><td>非ie瀏覽器完全可以用</td><td>西安</td><td>admin@kali.com</td></tr><tr><td>2</td><td>guest</td><td>23</td><td>ie瀏覽器不太確定</td><td>北京</td><td>guest@kali.com</td></tr> </table> <input id="Button1" type="button" value="導出EXCEL" οnclick="javascript:method1('ta')" /> </body> </html>
總結
以上是生活随笔為你收集整理的js页面上的excel导出的全部內容,希望文章能夠幫你解決所遇到的問題。