【发布时间】:2021-03-09 07:37:46
【问题描述】:
我正在尝试将 HTML 表格导出为 xls 格式。但是当我尝试使用 Excel 打开文件时,会收到一条警告消息。警告信息:
我使用这行代码:
var file = new Blob([ html.outerHTML], {
type: "application/vnd.ms-excel"
});
var url = URL.createObjectURL(file);
var filename = dateSelected + "-" + "attendance" + ".xls"
//here we are creating HTML <a> Tag which can be trigger to download the excel file.
var a = document.createElement('a');
a.id = "export";
document.body.appendChild(a);
//here we are checking if the bwoswer is IE or not if IE then we use window.navigator.msSaveBlob function otherwise Go with Simple Blob file.
if (window.navigator && window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(file, filename);
a.click();
document.body.removeChild(a);
document.body.removeChild(html);
} else {
a.download = filename;
a.href = url;
a.click();
document.body.removeChild(a);
document.body.removeChild(html);
}
我尝试更改 blob 类型,但没有任何效果。我该如何解决这个问题?
【问题讨论】:
-
点击“是”后是否打开文件?
-
@PalashKantiBachar 是的...文件正在打开...并且该文件具有excel的所有功能...但是在打开之前会发出警告
-
尝试将扩展名更改为
.xlsx,它不应该给出警告,因为它不能包含宏。
标签: javascript excel