gpt4 book ai didi

javascript - 文件格式或文件扩展名无效

转载 作者:行者123 更新时间:2023-11-30 15:54:20 25 4
gpt4 key购买 nike

我正在使用 rainabba/jquery-table2excel将 html 表导出到 excel。单击按钮,文件将下载,但 excel 报告文件或格式已损坏。请提出建议。

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery-table2excel-master/dist/jquery.table2excel.min.js"></script>

<script type="text/javascript">
$(document).ready(function(e){
$("#myButton").click(function (e){
$(".mytable").table2excel({
name:"new File",
filename:"work",
//fileext:".xlsx"

});

});

});


</script>
</head>
<body>
<button id="myButton">Click To Download</button>
<table class="mytable" border="1">
<tr>
<th>new1</th>
<th>new2</th>
<th>new3</th>
<th>new4</th>

</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>

最佳答案

<button class="btn btn-default" onclick="tableToExcel(\'#divId\', \'Excel Report\')">Generate Excel</Button>
<div id="divId">
<table class="mytable" border="1">
<tr>
<th>new1</th>
<th>new2</th>
<th>new3</th>
<th>new4</th>

</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</div>

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><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))
}
})()

使用此代码..生成 excel 文件..

关于javascript - 文件格式或文件扩展名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com