JS 直接导出excel 兼容ie、chrome、firefox很好用但是不能改下载的文件名

  1 <html>
  2 <head>
  3 <script type="text/javascript" language="javascript">
  4         var idTmr;
  5         function  getExplorer() {
  6             var explorer = window.navigator.userAgent ;
  7             //ie 
  8             if (explorer.indexOf("MSIE") >= 0) {
  9                 return 'ie';
 10             }
 11             //firefox 
 12             else if (explorer.indexOf("Firefox") >= 0) {
 13                 return 'Firefox';
 14             }
 15             //Chrome
 16             else if(explorer.indexOf("Chrome") >= 0){
 17                 return 'Chrome';
 18             }
 19             //Opera
 20             else if(explorer.indexOf("Opera") >= 0){
 21                 return 'Opera';
 22             }
 23             //Safari
 24             else if(explorer.indexOf("Safari") >= 0){
 25                 return 'Safari';
 26             }
 27         }
 28         function method1(tableid) {//整个表格拷贝到EXCEL中
 29             if(getExplorer()=='ie')
 30             {
 31                 var curTbl = document.getElementById(tableid);
 32                 var oXL = new ActiveXObject("Excel.Application");
 33 
 34                 //创建AX对象excel 
 35                 var oWB = oXL.Workbooks.Add();
 36                 //获取workbook对象 
 37                 var xlsheet = oWB.Worksheets(1);
 38                 //激活当前sheet 
 39                 var sel = document.body.createTextRange();
 40                 sel.moveToElementText(curTbl);
 41                 //把表格中的内容移到TextRange中 
 42                 sel.select;
 43                 //全选TextRange中内容 
 44                 sel.execCommand("Copy");
 45                 //复制TextRange中内容  
 46                 xlsheet.Paste();
 47                 //粘贴到活动的EXCEL中       
 48                 oXL.Visible = true;
 49                 //设置excel可见属性
 50 
 51                 try {
 52                     var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
 53                 } catch (e) {
 54                     print("Nested catch caught " + e);
 55                 } finally {
 56                     oWB.SaveAs(fname);
 57 
 58                     oWB.Close(savechanges = false);
 59                     //xls.visible = false;
 60                     oXL.Quit();
 61                     oXL = null;
 62                     //结束excel进程,退出完成
 63                     //window.setInterval("Cleanup();",1);
 64                     idTmr = window.setInterval("Cleanup();", 1);
 65 
 66                 }
 67 
 68             }
 69             else
 70             {
 71                 tableToExcel('ta')//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 72             }
 73         }
 74         function Cleanup() {
 75             window.clearInterval(idTmr);
 76             CollectGarbage();
 77         }
 78         var tableToExcel = (function() {
 79               var uri = 'data:application/vnd.ms-excel;base64,',
 80               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>',
 81                 base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
 82                 format = function(s, c) {
 83                     return s.replace(/{(\w+)}/g,
 84                     function(m, p) { return c[p]; }) }
 85                 return function(table, name) {
 86                 if (!table.nodeType) table = document.getElementById(table)
 87                 var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
 88                 window.location.href = uri + base64(format(template, ctx))
 89               }
 90             })()
 91     </script>
 92 
 93 </head>
 94 <body>
 95 <table id="ta">//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 96   <tr>
 97     <td>1</td><td>admin</td>
 98     <td>23</td><td>程序员</td>
 99     <td>天津</td><td>admin@kali.com</td>
100   </tr>
101   <tr>
102     <td>2</td><td>guest</td>
103     <td>23</td><td>测试员</td>
104     <td>北京</td><td>guest@kali.com</td>
105   </tr>
106 </table>
107 <input id="Button1" type="button" value="导出EXCEL" 
108         onclick="javascript:method1('ta')" />//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1111
109 </body>
110 </html>

只需要注意table的名字即可

 

posted @ 2017-11-30 14:31  huanqicode  阅读(1109)  评论(0)    收藏  举报