![](/img/trans.png)
[英]How to export html table to excel with correct latitude/longitude format?
[英]How to export HTML data into Excel in correct format?
我正在使用jQuery
將HTML數據導出到Excel。 數據正在導出,但是沒有以正確的格式導出。 打開Excel文件時顯示:
“ download.xls”的文件格式和擴展名不匹配。 該文件可能已損壞或不安全。 除非您信任其來源,否則請不要打開它。 您是否仍要打開它?
當我打開它時,那里的數據存在了,但被嚴重地合並了。 我該怎么辦?
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 \" />";
}
這是我的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>
試試這個代碼:
$(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.