最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Export to Excel doesn't work in edge browser - Stack Overflow

matteradmin5PV0评论

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 user9240245user9240245
Add a ment  | 

1 Answer 1

Reset to default 5

You 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');
Post a comment

comment list (0)

  1. No comments so far