[英]Exporting html table to excel file (.xlsx), getting corrupted file on download
[英]Download Excel file in xlsx format using Javascript (exporting html tables into Excel)
我正在使用 JS、HTML 和 CSS 來構建我的應用程序並將其托管在服務器上。 我正在使用以下代碼將所有 html 表導入到 xls 格式的 excel 文件中。
function fnExcelReport(tableNames) {
var tab_text = "";
var arrTableNames = tableNames.split("|");
if (arrTableNames.length > 0) {
for (var i = 0; i < arrTableNames.length; i++) {
tab_text = tab_text + "<table border='1px'>";
tab = document.getElementById(arrTableNames[i]);
for (j = 0; j < tab.children.length; j++) {
tab_text = tab_text + tab.children[j].innerHTML;
}
tab_text = tab_text + "</table><br/><br/>";
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
{
if (window.navigator.msSaveBlob) {
var blob = new Blob([tab_text], {
type: "data:application/vnd.ms-excel;"
});
sa = navigator.msSaveBlob(blob, "report.xls");
}
} else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
我可以下載該文件並打開它,但在打開它時收到以下警告消息:
當我單擊是時,我的所有數據都正確顯示在 Excel 文件中。 我不想看到那個警告信息。 如何更改我的代碼,使其以 xlsx 格式打開並刪除警告消息?
這是因為內容不是“xls”類型。 即如果 excel 是“xlsx”類型,您將收到此錯誤。 嘗試使用其他帶有文件名的 excel 擴展名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.