Excel导出不适用于Firefox,但在Google Crome中可以正常工作

1 html javascript export-to-excel

调用以下给定事件将表中的数据导出到EXCEL中,该代码的工作原理类似于Chrome中的超级按钮。在IE和Firefox中,我什么也没收到(文件,错误等)。请协助我在所有浏览器中导出文件

$("[id$=myButtonControlID]").click(function(e) { 
    var result = 'data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=printHead]').html());
    var link = document.createElement("a");
    link.download = "Reports";
    link.href = result;
    link.click();
});
Run Code Online (Sandbox Code Playgroud)

gus*_*s27 7

使用Firefox,必须link先将元素显式添加到DOM,然后才能执行.click()

$("[id$=myButtonControlID]").click(function(e) { 
    var result = 'data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=printHead]').html());
    var link = document.createElement("a");
    document.body.appendChild(link);  // You need to add this line
    link.download = "Reports";
    link.href = result;
    link.click();
});
Run Code Online (Sandbox Code Playgroud)

data:URI是从IE8的支持。但是它“不能用于导航”,因此我认为它在…中将不起作用<a href="...">。请参阅此链接