將表格導出爲excel

 1 <table id="tableExcel" border="1">
 2     <tr>
 3         <th>零</th>
 4         <th>一</th>
 5         <th>二</th>
 6         <th>三</th>
 7         <th>四</th>
 8     </tr>
 9     <tr>
10         <td>萬籟寂無聲</td>
11         <td>衾鐵棱棱近五更</td>
12         <td>香斷燈昏吟未穩</td>
13         <td>悽清</td>
14         <td>只有霜華伴月明</td>
15     </tr>
16     <tr>
17         <td>應是夜寒凝</td>
18         <td>惱得梅花睡不成</td>
19         <td>我念梅花花念我</td>
20         <td>關情</td>
21         <td>起看清冰滿玉瓶</td>
22     </tr>
23 </table>
24 <input type="button" value="導出EXCEL" onclick="exportExcel('tableExcel')"/>
  1     var idTmr;
  2 
  3     //獲取當前瀏覽器類型
  4     function getExplorer() {
  5         var explorer = window.navigator.userAgent;
  6         //ie
  7         if (explorer.indexOf("MSIE") >= 0) {
  8             return 'ie';
  9         }
 10         //firefox
 11         else if (explorer.indexOf("Firefox") >= 0) {
 12             return 'Firefox';
 13         }
 14         //Chrome
 15         else if (explorer.indexOf("Chrome") >= 0) {
 16             return 'Chrome';
 17         }
 18         //Opera
 19         else if (explorer.indexOf("Opera") >= 0) {
 20             return 'Opera';
 21         }
 22         //Safari
 23         else if (explorer.indexOf("Safari") >= 0) {
 24             return 'Safari';
 25         }
 26     }
 27 
 28     //獲取到類型需要判斷當前瀏覽器需要調用的方法,目前項目中火狐,谷歌,360沒有問題
 29     //win10自帶的IE無法導出
 30     function exportExcel(tableid) {//整個表格拷貝到EXCEL中
 31         if (getExplorer() == 'ie') {
 32             var curTbl = document.getElementById(tableid);
 33             var oXL = new ActiveXObject("Excel.Application");
 34 
 35             //創建AX對象excel
 36             var oWB = oXL.Workbooks.Add();
 37             //獲取workbook對象
 38             var xlsheet = oWB.Worksheets(1);
 39             //激活當前sheet
 40             var sel = document.body.createTextRange();
 41             sel.moveToElementText(curTbl);
 42             //把表格中的內容移到TextRange中
 43             sel.select;
 44             //全選TextRange中內容
 45             sel.execCommand("Copy");
 46             //複製TextRange中內容
 47             xlsheet.Paste();
 48             //粘貼到活動的EXCEL中
 49             oXL.Visible = true;
 50             //設置excel可見屬性
 51 
 52             try {
 53                 var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
 54             } catch (e) {
 55                 print("Nested catch caught " + e);
 56             } finally {
 57                 oWB.SaveAs(fname);
 58 
 59                 oWB.Close(savechanges = false);
 60                 //xls.visible = false;
 61                 oXL.Quit();
 62                 oXL = null;
 63                 //結束excel進程,退出完成
 64                 //window.setInterval("Cleanup();",1);
 65                 idTmr = window.setInterval("Cleanup();", 1);
 66             }
 67         } else {
 68             tableToExcel(tableid)
 69         }
 70     }
 71 
 72     function Cleanup() {
 73         window.clearInterval(idTmr);
 74         CollectGarbage();
 75     }
 76 
 77     /*
 78         template : 定義文檔的類型,相當於html頁面中頂部的<!DOCTYPE> 聲明。(個人理解,不確定)
 79 
 80         encodeURIComponent:解碼
 81 
 82         unescape() 函數:對通過 escape() 編碼的字符串進行解碼。
 83 
 84         window.btoa(window.encodeURIComponent(str)):支持漢字進行解碼。
 85 
 86         \w :匹配包括下劃線的任何單詞字符。等價於’[A-Za-z0-9_]’
 87 
 88         replace()方法:用於在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。
 89 
 90         {(\w+)}:匹配所有 {1個或更多字符} 形式的字符串;此處匹配輸出內容是 “worksheet”
 91 
 92         正則中的() :是爲了提取匹配的字符串。表達式中有幾個()就有幾個相應的匹配字符串。
 93 
 94         講解(/{(\w+)}/g, function(m, p) { return c[p]; } :
 95 
 96             /{(\w+)}/g 匹配出所有形式爲“{worksheet}”的字符串;
 97 
 98             function參數:   m  正則所匹配到的內容,即“worksheet”;
 99                             p  正則表達式中分組的內容,即“(\w+)”分組中匹配到的內容,爲“worksheet”;
100 
101             c :爲object,見下圖3
102 
103             c[p] : 爲“worksheet”
104     */
105 
106 
107     //判斷瀏覽器後調用的方法,把table的id傳入即可
108     var tableToExcel = (function () {
109         var uri = 'data:application/vnd.ms-excel;base64,',
110             template = '<html><head><meta charset="UTF-8"></head><body><table border="1">{table}</table></body></html>',
111             base64 = function (s) {
112                 return window.btoa(unescape(encodeURIComponent(s)))
113             },
114             // 下面這段函數作用是:將template中的變量替換爲頁面內容ctx獲取到的值
115             format = function (s, c) {
116                 return s.replace(/{(\w+)}/g,
117                     function (m, p) {
118                         return c[p];
119                     }
120                 )
121             };
122         return function (table, name) {
123             if (!table.nodeType) {
124                 table = document.getElementById(table)
125             }
126             // 獲取表單的名字和表單查詢的內容
127             var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
128             // format()函數:通過格式操作使任意類型的數據轉換成一個字符串
129             // base64():進行編碼
130             window.location.href = uri + base64(format(template, ctx))
131         }
132     })()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章