
正文
使用纯js 不导包实现 table 导出 Excel
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.将js粘贴到项目
2.设置table标签 id
3.定义按钮,调用方法即可
1 <!DOCTYPE html>
2 <html lang="zh_CN">
3 <head>
4 <meta charset="UTF-8">
5 <title>导出Excel</title>
6 </head>
7 <body>
8 <button type="button" onclick="method5('tableExcel')">导出Excel</button>
9 <table border=6 frame=void align=center width=650px height=100px cellspacing=0px cellpadding=10px id="tableExcel">
10 <tr height=50px align=center>
11 <th width=110px></th>
12 <th><font color=blue>星期一</font></th>
13 <th><font color=blue>星期二</font></th>
14 <th><font color=blue>星期三</font></th>
15 <th><font color=blue>星期四</font></th>
16 <th><font color=blue>星期五</font></th>
17 </tr>
18 <tr height=40px align=center>
19 <td></td>
20 <td colspan=5 color=red><font color="red"><b>上午</b></font></td>
21 </tr>
22 <tr height=40px align=center>
23 <td><font color=blue><b>一</b></font></td>
24 <td>数学</td>
25 <td>英语</td>
26 <td>语文</td>
27 <td>文体活动</td>
28 <td>数学</td>
29 </tr>
30 <tr height=40px align=center>
31 <td><font color=blue><b>二</b></font></td>
32 <td>语文</td>
33 <td>美术</td>
34 <td>语文</td>
35 <td>语文</td>
36 <td>语文</td>
37 </tr>
38 <tr height=40px align=center>
39 <td><font color=blue><b>三</b></font></td>
40 <td>体育</td>
41 <td>文体活动</td>
42 <td>文体活动</td>
43 <td>品德与生活</td>
44 <td>英语</td>
45 </tr>
46 <tr height=40px align=center>
47 <td></td>
48 <td colspan=5><font color="red"><b>下午</b></font></td>
49 </tr>
50 <tr height=40px align=center>
51 <td><font color=blue><b>四</b></font></td>
52 <td>品德与生活</td>
53 <td>语文</td>
54 <td>数学</td>
55 <td>体育</td>
56 <td>美术</td>
57 </tr>
58 <tr height=40px align=center>
59 <td><font color=blue><b>五</b></font></td>
60 <td>文体活动</td>
61 <td>体育</td>
62 <td>专题</td>
63 <td>语文</td>
64 <td>语文</td>
65 </tr>
66 <tr height=40px align=center>
67 <td><font color=blue><b>六</b></font></td>
68 <td>兴趣</td>
69 <td>读书活动</td>
70 <td>音乐</td>
71 <td>音乐</td>
72 <td>数学</td>
73 </tr>
74 <tr height=40px align=center>
75 <td><font color=blue><b>七</b></font></td>
76 <td>班队活动</td>
77 <td>课后托管</td>
78 <td>课后托管</td>
79 <td>课后托管</td>
80 <td>课后托管</td>
81 </tr>
82 </table>
83
84 <script>
85 //导出excel
86 var idTmr;
87 function getExplorer() {
88 var explorer = window.navigator.userAgent ;
89 //ie
90 if (explorer.indexOf("MSIE") >= 0) {
91 return 'ie';
92 }
93 //firefox
94 else if (explorer.indexOf("Firefox") >= 0) {
95 return 'Firefox';
96 }
97 //Chrome
98 else if(explorer.indexOf("Chrome") >= 0){
99 return 'Chrome';
100 }
101 //Opera
102 else if(explorer.indexOf("Opera") >= 0){
103 return 'Opera';
104 }
105 //Safari
106 else if(explorer.indexOf("Safari") >= 0){
107 return 'Safari';
108 }
109 }
110 function method5(tableid) {
111 if(getExplorer()=='ie')
112 {
113 var curTbl = document.getElementById(tableid);
114 var oXL = new ActiveXObject("Excel.Application");
115 var oWB = oXL.Workbooks.Add();
116 var xlsheet = oWB.Worksheets(1);
117 var sel = document.body.createTextRange();
118 sel.moveToElementText(curTbl);
119 sel.select();
120 sel.execCommand("Copy");
121 xlsheet.Paste();
122 oXL.Visible = true;
123
124 try {
125 var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
126 } catch (e) {
127 print("Nested catch caught " + e);
128 } finally {
129 oWB.SaveAs(fname);
130 oWB.Close(savechanges = false);
131 oXL.Quit();
132 oXL = null;
133 idTmr = window.setInterval("Cleanup();", 1);
134 }
135
136 }
137 else
138 {
139 tableToExcel(tableid)
140 }
141 }
142 function Cleanup() {
143 window.clearInterval(idTmr);
144 // CollectGarbage();
145 }
146 var tableToExcel = (function() {
147 var uri = 'data:application/vnd.ms-excel;base64,',
148 template = '<html><head><meta charset="UTF-8"></head><body><table border="1px">{table}</table></body></html>',
149 base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
150 format = function(s, c) {
151 return s.replace(/{(\w+)}/g,
152 function(m, p) { return c[p]; }) }
153 return function(table, name) {
154 if (!table.nodeType) table = document.getElementById(table)
155 var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
156 window.location.href = uri + base64(format(template, ctx))
157 }
158 })();
159 </script>
160 </body>
161 </html>







