<body>
<a href="javascript:exportFile()">导出</a>
<table id="showTable" border="1" width="100%">
<c:forEach begin="0" end="5000" var="i">
<tr>
<c:forEach begin="0" end="30" var="j">
<!-- mso-number-format:'@' 数字以文本方式显示 -->
<td style="text-align:center; mso-number-format:'@';">${i }${j }</td>
</c:forEach>
</tr>
</c:forEach>
</table>
<script type="text/javascript">
/**
* 表格导出
* style="mso-number-format:'@';"
* 1) 文本:'@'
* 2) 日期:'yyyy/mm/dd'
* 3) 数字:'#,##0.00'
* 4) 货币:'¥#,##0.00'
* 5) 百分比:'#0.00%'
*/
function exportFile(){
//获取表格
var exportFileContent = document.getElementById("showTable").outerHTML;
//设置格式为Excel,表格内容通过btoa转化为base64,此方法只在文件较小时使用(小于1M)
//exportFileContent = window.btoa(unescape(encodeURIComponent(exportFileContent)));
//var link = "data:"+MIMEType+";base64," + exportFileContent;
//使用Blob
var blob = new Blob([exportFileContent], {type: "text/plain;charset=utf-8"});
//解决中文乱码问题
blob = new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
//设置链接
var link = window.URL.createObjectURL(blob);
var a = document.createElement("a"); //创建a标签
a.download = "数据展示.xls"; //设置被下载的超链接目标(文件名)
a.href = link; //设置a标签的链接
document.body.appendChild(a); //a标签添加到页面
a.click(); //设置a标签触发单击事件
document.body.removeChild(a); //移除a标签
}
</script>
</body>
页面
下载
下载后的Excel