【发布时间】:2016-07-05 08:58:45
【问题描述】:
我们有一些将数据导出到 Excel 文件的功能。 单击“导出”按钮时,会调用一些客户端javascript,首先检查客户端浏览器版本,并据此决定以哪种方式呈现excel文档。 在本地测试时,它可以在 Chrome & Firefox & IE11 中运行。 但是,当我使用运行 Edge 浏览器的 Windows 10 机器进行远程测试时,不会呈现 excel。
我可能会补充一点,我的本地机器是 Win7 机器,我正在运行 VS2012 和 IE11。远程机器是Win10 with Edge,因此需要远程测试。 我已经在 IE11 F12 开发工具中尝试过仿真,但无法在那里复制 Edge 错误。
使用以下代码时,'open' 会抛出 'undefined or null reference' 的错误:
excelIFrame.document.open("txt/html", "replace");
excelIFrame.document.write(sHTML);
excelIFrame.document.close();
excelIFrame.focus();
excelIFrame.document.execCommand("SaveAs", true, "Spreadsheet.xls");
iframe存在于html中,不是动态添加的。
<iframe id="excelIFrame" style="display:none"></iframe>
我尝试了以下可能的解决方案来使其正常工作,但无济于事 -
可能的解决方案 1: 将文档分配给临时变量时出现相同的“未定义或空引用”错误
var doc = excelIFrame.document;
doc.open("txt/html", "replace");
doc.write(sHTML);
doc.close();
doc.focus();
doc.execCommand("SaveAs", true, "Spreadsheet.xls");
可能的解决方案 2: 使用 iFrame 的 contentWindow 属性。没有抛出错误,它只是打开不包含任何内容的“about:blank”。
excelIFrame.contentWindow.contents = sHTML;
excelIFrame.src = 'javascript:window["contents"]';
在这个阶段完全不知所措。 该页面是一个 angularJS 网页。 通过阅读它,我知道使用 iframe 时 document.open 在边缘存在问题。但是下面的链接document.open fails in an iframe我觉得可以解决问题。 非常感谢任何想法或建议。
【问题讨论】:
标签: javascript angularjs excel iframe microsoft-edge