答案 0 :(得分:0)
您可以尝试以下功能:
function ExportToExcel(fileName)
{
var isIE = (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0);
if (isIE) {
// IE > 10
if (typeof Blob != 'undefined') {
var fileData = new Blob([document.getElementById("datatable").innerHTML.replace(/=" "/gi, "")], {type: 'application/vnd.ms-excel,'});
window.navigator.msSaveBlob(fileData, fileName + '.xls');
}
// IE < 10
else {
myFrame.document.open("text/html", "replace");
myFrame.document.write(document.getElementById("datatable").innerHTML.replace(/=" "/gi, ""));
myFrame.document.close();
myFrame.focus();
myFrame.document.execCommand('SaveAs', true, fileName + '.xls');
}
}
// crome,mozilla
else {
var uri = 'data:application/vnd.ms-excel,' + document.getElementById("datatable").innerHTML.replace(/ /g, '%20');
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
为了使此功能正常工作,在旧的IE浏览器中,您需要具有名称 myFrame 的 iframe ,这是此功能中使用的帧。还要确保将表格包装在 div 中,在此片段中,div的id为 datatable