您的位置:首页 > Web前端 > JavaScript

chrome浏览器js 导出excel

2016-12-13 15:01 357 查看
<table id="table">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
<th>地区</th>
<th>邮箱</th>
</tr>
<tr>
<td>1</td>
<td>admin</td>
<td>23</td>
<td>程序员</td>
<td>天津</td>
<td>admin@kali.com</td>
</tr>
<tr>
<td>2</td>
<td>guest</td>
<td>23</td>
<td>测试员</td>
<td>北京</td>
<td>guest@kali.com</td>
</tr>
</table>
<input id="Button1" type="button" value="导出EXCEL"onclick="tableToExcel('table','测试')" />


var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
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 version="1.0" encoding="UTF-8" standalone="yes"?><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>',
base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
format = function (s, c) {
return s.replace(/{(\w+)}/g,
function (m, p) { return c[p]; })
}
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: