1 <table id="table">
2 <tr>
3 <th>IDth>
4 <th>姓名th>
5 <th>年龄th>
6 <th>职业th>
7 <th>地区th>
8 <th>邮箱th>
9 tr>
10 <tr>
11 <td>1td>
12 <td>admintd>
13 <td>23td>
14 <td>程序员td>
15 <td>天津td>
16 <td>admin@kali.comtd>
17 tr>
18 <tr>
19 <td>2td>
20 <td>guesttd>
21 <td>23td>
22 <td>测试员td>
23 <td>北京td>
24 <td>guest@kali.comtd>
25 tr>
26 table>
27 <input id="Button1" type="button" value="导出EXCEL"onclick="tableToExcel('table','测试')" />
1 var tableToExcel = (function () {
2 var uri = 'data:application/vnd.ms-excel;base64,',
3 template = '
'
,
4 base64 =
function (s) {
return window.btoa(unescape(encodeURIComponent(s))) },
5 format =
function (s, c) {
6 return s.replace(/{(\w+)}/
g,
7 function (m, p) {
return c[p]; })
8 }
9 return function (table, name) {
10 if (!table.nodeType) table =
document.getElementById(table)
11 var ctx = { worksheet: name || 'Worksheet'
, table: table.innerHTML }
12 window.location.href = uri +
base64(format(template, ctx))
13 }
14 })()