javascript 如何以正确的格式将 HTML 数据导出到 Excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30972326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:01:57  来源:igfitidea点击:

How to export HTML data into Excel in correct format?

javascriptphpjqueryhtmlexcel

提问by babar

I am using jQueryto export HTML data into Excel. Data is exporting, but it's not exporting in a correct format. On opening the Excel file it says:

我正在使用jQuery将 HTML 数据导出到 Excel。正在导出数据,但导出的格式不正确。打开 Excel 文件时,它说:

The file format and extension of "download.xls" don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?

“download.xls”的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它。还是要打开它?

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>

采纳答案by WisdmLabs

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

回答by WisdmLabs

Change the .xls to .xlsx if you have the latest office installed.

如果您安装了最新的 office,请将 .xls 更改为 .xlsx。