js导出网页table为Excel文件的问题

WhiteDouglas WhiteDouglas 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
通过下面这种js脚本导出的Excel文件在保存到本地并打开时,office会弹出提示:

请问如何修改js脚本才能解决弹出提示的问题?另外最好能通过js生成xlsx后缀的文件~
<!DOCTYPE>
<html>
<head>
<title>Excel Test</title>
</head>

<body>
<div style="width:100%;padding:40px;">
Excel Test
</div>
<table id="excel">
<tr>
<td>Name</td>
<td>Age</td>
<td>Gender</td>
</tr>
<tr>
<td>Young</td>
<td>24</td>
<td>Male</td>
</tr>
<tr>
<td>Even</td>
<td>23</td>
<td>Female</td>
</tr>
<tr>
<td>Leon</td>
<td>25</td>
<td>Male</td>
</tr>
</table>
</body>
<div style="width:200px;margin:auto;text-align:center;">
<button onclick="ok()">导出</button>
</div>

<script>
var ok = (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) {
var tables = document.getElementById('excel');
var ctx = { worksheet: name || 'Worksheet', table: tables.innerHTML }
window.location.href = uri + base64(format(template, ctx));
}
})();

function ExportSupplierMonthlyData() {
try {
tableToExcel();
} catch (err) {
bootbox.alert('没有数据,导出失败');
}
}
</script>
</html>
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
WhiteDouglas
JavaScript 87,983 成员 224,681 内容