此代码对于我的 Web 应用程序来说几乎完美无缺。感谢sampopes关于 this thread 的答案.
function fnExcelReport()
{
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('headerTable'); // id of table
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
}
else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
基本上,我有一个 HTML 表(它由 handlebars 和 mysql 使用数据库中的数据“填充”,但我们可以将其排除在等式之外并考虑使用静态表)。用户可以在当前版本中保存表格,但文件名始终为“download.xls”(IE 7+ 除外)。我希望用户能够在导出/下载文件时定义文件名。
但是有一个问题。我不能再使用任何插件(将其视为限制)
最佳答案
You Can Use Data Table for export table data
visit link
https://datatables.net/extensions/buttons/examples/initialisation/export.html
关于javascript - 使用另存为对话框将 HTML 表格数据导出到 Excel (JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38312476/