js到处excel

时间:2022-01-13 14:35:03
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>js把table导出xls</title>
</head>
<body>
<table id="excel" width='100%' border='0' align='center' cellpadding='5' cellspacing='1' bgcolor='#B3B3B3'>
<tr>
<th width='120' bgcolor='#EBEBEB'>账目分类</th>
<th width='50' bgcolor='#EBEBEB'>金额</th>
<th width='90' bgcolor='#EBEBEB'>支出/收入</th>
<th width='150' bgcolor='#EBEBEB'>时间</th>
<th width='60' bgcolor='#EBEBEB'>备注</th>
</tr> <tr>
<td align='center' bgcolor='#FFFFFF'>cabildo</td>
<td align='center' bgcolor='#FFFFFF'>56</td><td align='center' bgcolor='#FFFFFF'><font color='blue'>收入</font></td><td align='center' bgcolor='#FFFFFF'>2014-09-19 2:41</td><td align='center' bgcolor='#FFFFFF'>123</td></tr><tr>
<td align='center' bgcolor='#FFFFFF'>rivadavia</td>
<td align='center' bgcolor='#FFFFFF'>61</td><td align='center' bgcolor='#FFFFFF'><font color='blue'>收入</font></td><td align='center' bgcolor='#FFFFFF'>2014-09-19 2:42</td><td align='center' bgcolor='#FFFFFF'>123</td></tr> </table><br />注意:导出后的文件用电脑的记事本方式打开,另存为Unicode编码,然后显示就正常了。<br /><br /> <input type="button" onclick="daochu('excel')" value="导出搜索结果为xls excel文件">
</body>
<script type="text/javascript">
var daochu = (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><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))
}
})()
</script>
</html>