admin管理员组文章数量:1432594
This is the javascript function
function fnExcelReport()
{
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('tableID'); // id of table
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text=tab_text+"</table>";
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"filename.xls");
}
else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
and it will invoked on button click
<button id="btnExport" onclick="fnExcelReport();"> EXPORT </button>
This code works in Chrome, Firefox and IE. This is not working in Edge. Please help.
This is the javascript function
function fnExcelReport()
{
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
tab = document.getElementById('tableID'); // id of table
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text=tab_text+"</table>";
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"filename.xls");
}
else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
and it will invoked on button click
<button id="btnExport" onclick="fnExcelReport();"> EXPORT </button>
This code works in Chrome, Firefox and IE. This is not working in Edge. Please help.
Share Improve this question asked Jan 22, 2018 at 6:48 user9240245user92402451 Answer
Reset to default 5You are not catching Edge in your if
Edge will not have MSIE in the window.navigator.userAgent; it will have something like this
Mozilla/5.0 (Windows NT 10.0; <64-bit tags>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev> Edge/<EdgeHTML Rev>.<Windows Build>
So instead of checking for MSIE, you should be checking for Edge
Refer to this MS link User-agent string changes
Also take a look here
Refer to this fiddle https://jsfiddle/p8o42kdh/
Couple of points. 1) instead of indexOf I used match
ua.match(/Edge/)
2) To create file content I used blob
var blob = new Blob(["tab_text"], {type: 'data:application/vnd.ms-excel'});
window.navigator.msSaveBlob(blob, 'msSaveBlob_testFile.xls');
本文标签: javascriptExport to Excel doesn39t work in edge browserStack Overflow
版权声明:本文标题:javascript - Export to Excel doesn't work in edge browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604678a2665769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论