[英]How to export HTML data into Excel in correct format?
I am using jQuery
to export HTML data into Excel. 我正在使用jQuery
将HTML数据导出到Excel。 Data is exporting, but it's not exporting in a correct format. 数据正在导出,但是没有以正确的格式导出。 On opening the Excel file it says: 打开Excel文件时显示:
The file format and extension of "download.xls" don't match. “ download.xls”的文件格式和扩展名不匹配。 The file could be corrupted or unsafe. 该文件可能已损坏或不安全。 Unless you trust its source, don't open it. 除非您信任其来源,否则请不要打开它。 Do you want to open it anyway? 您是否仍要打开它?
And when I open it, the data there is present, but badly merged. 当我打开它时,那里的数据存在了,但被严重地合并了。 What should I do? 我该怎么办?
echo "<div id=dvData3>";
//echo "<table border=5 cellpadding=5 cellspacing=0 style=border-collapse: collapse >";
echo "<table>";
echo "<tr>";
echo "<td width=14% align=center>Name</td>";
echo "<td width=14% align=center>Grand Total</td>";
echo "<td width=14% align=center>Expanse Type</td>";
echo "</tr>";
while($allrows=mysql_fetch_array($newresult1))
{
$name=$allrows["empid"];
$gamount=$allrows["gtotal"];
$exptype=$allrows["expanseType"];
echo "<tr>";
echo "<td width=14% align=center>$name</td>";
echo "<td width=14% align=center>$gamount</td>";
echo "<td width=14% align=center>$exptype</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td width=14% align=center>Total Amount</td>";
echo "<td width=14% align=center>$Tamount</td>";
echo "</table>";
echo "</div>";
echo "<input type='button' id=\"btnExport3\" value=\" Export Table data into Excel \" />";
}
And here's my jQuery function: 这是我的jQuery函数:
<script>
$(document).ready(function(){
$("#btnExport3").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#dvData3').html());
//window.open('data:application/csv,charset=utf-8,' + $('#dvData').html());
e.preventDefault();
});
});
</script>
Try this code: 试试这个代码:
$(document).ready(function() {
$("#btnExport3").click(function(e) {
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData3');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = 'download.xlsx';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
});
});
如果您安装了最新的Office,请将.xls更改为.xlsx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.