JS HTML table 生成 Excel文件

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Document</title>
  6 </head>
  7 <body>
  8     <table id='tab'>
  9         <tr>
 10             <td>1</td>
 11             <td>1</td>
 12             <td>1</td>
 13         </tr>
 14         <tr>
 15             <td>2</td>
 16             <td>2</td>
 17             <td>2</td>
 18         </tr>
 19         <tr>
 20             <td>3</td>
 21             <td>3</td>
 22             <td>3</td>
 23         </tr>
 24     </table>
 25     <button onclick="ecxelMethod('tab')">tabtoexcel</button>
 26 </body>
 27 </html>
 28 <script>
 29     //导出Excel方法
 30     var idTmr;
 31     function  getExplorer() {
 32         var explorer = window.navigator.userAgent ;
 33         //ie
 34         if (explorer.indexOf("MSIE") >= 0) {
 35             return 'ie';
 36         }
 37         //firefox
 38         else if (explorer.indexOf("Firefox") >= 0) {
 39             return 'Firefox';
 40         }
 41         //Chrome
 42         else if(explorer.indexOf("Chrome") >= 0){
 43             return 'Chrome';
 44         }
 45         //Opera
 46         else if(explorer.indexOf("Opera") >= 0){
 47             return 'Opera';
 48         }
 49         //Safari
 50         else if(explorer.indexOf("Safari") >= 0){
 51             return 'Safari';
 52         }
 53     }
 54     function ecxelMethod(tableid) {
 55         if(getExplorer()=='ie')
 56         {
 57             var curTbl = document.getElementById(tableid);
 58             var oXL = new ActiveXObject("Excel.Application");
 59             var oWB = oXL.Workbooks.Add();
 60             var xlsheet = oWB.Worksheets(1);
 61             var sel = document.body.createTextRange();
 62             sel.moveToElementText(curTbl);
 63             sel.select();
 64             sel.execCommand("Copy");
 65             xlsheet.Paste();
 66             oXL.Visible = true;
 67 
 68             try {
 69                 var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
 70             } catch (e) {
 71                 print("Nested catch caught " + e);
 72             } finally {
 73                 oWB.SaveAs(fname);
 74                 oWB.Close(savechanges = false);
 75                 oXL.Quit();
 76                 oXL = null;
 77                 idTmr = window.setInterval("Cleanup();", 1);
 78             }
 79 
 80         }
 81         else
 82         {
 83             tableToExcel(tableid)
 84         }
 85     }
 86     function Cleanup() {
 87         window.clearInterval(idTmr);
 88         CollectGarbage();
 89     }
 90     var tableToExcel = (function() {
 91         var uri = 'data:application/vnd.ms-excel;base64,',
 92             template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
 93             base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
 94             format = function(s, c) {
 95                 return s.replace(/{(\w+)}/g,
 96                     function(m, p) { return c[p]; }) }
 97         return function(table, name) {
 98             if (!table.nodeType) table = document.getElementById(table)
 99             var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
100             window.location.href = uri + base64(format(template, ctx))
101         }
102     })()
103 
104 
105 </script>

 

posted @ 2017-07-28 13:58  三尺微命---  阅读(1405)  评论(0编辑  收藏  举报