html - Export table to MS Excel with Javascript, on MS Edge not working - Stack Overflow

I've been exporting a table from HTML to Excel using the very same code shown in this link and it

I've been exporting a table from HTML to Excel using the very same code shown in this link and it worked pretty good in many browsers.

The problem es when I test it on the brand new MS Edge web browser, it opens up a new blank tab, just that. No console error, no warning pop-up, nothing. As I said, in that link there's a way to handle exportation to Excel in IE.

So I wonder if anyone of you knows a similar trick to support the new Microsoft Edge browser.

Thanks.

I've been exporting a table from HTML to Excel using the very same code shown in this link and it worked pretty good in many browsers.

The problem es when I test it on the brand new MS Edge web browser, it opens up a new blank tab, just that. No console error, no warning pop-up, nothing. As I said, in that link there's a way to handle exportation to Excel in IE.

So I wonder if anyone of you knows a similar trick to support the new Microsoft Edge browser.

Thanks.

Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Dec 1, 2015 at 20:16 gingerEdgingerEd 832 silver badges4 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

You cannot presently navigate to data-urls in Internet Explorer, or Microsoft Edge, for security purposes. However, blobs can be downloaded or saved using msSaveBlob or msSaveOrOpenBlob.

I've prepared a basic example for you below:

(function () {

  // Generate our CSV string from out HTML Table
  var csv = tableToCSV( document.querySelector( "#sites" ) );
  // Create a CSV Blob
  var blob = new Blob( [ csv ], { type: "text/csv"} );

  // Determine which approach to take for the download
  if ( navigator.msSaveOrOpenBlob ) {
    // Works for Internet Explorer and Microsoft Edge
    navigator.msSaveOrOpenBlob( blob, "output.csv" );

  } else {

    // Attempt to use an alternative method
    var anchor = document.body.appendChild(
      document.createElement( "a" )
    );
    // If the [download] attribute is supported, try to use it
    if ( "download" in anchor ) {
      anchor.download = "output.csv";
      anchor.href = URL.createObjectURL( blob );
      anchor.click();
    }

  }

  function tableToCSV( table ) {
    // We'll be co-opting `slice` to create arrays
    var slice = Array.prototype.slice;

    return slice.call( table.rows ).map(function ( row ) {
      return slice.call( row.cells ).map(function ( cell ) {
        return '"t"'.replace( "t", cell.textContent );
      }).join( "," );
    }).join( "\r\n" );

  }

}());

Test Online: http://jsfiddle/jonathansampson/nc4k4hz8/

You'll want to perform a bit of feature detection to see if msSaveBlob or msSaveOrOpenBlob are available. If they are, use them, and if they're not you can proceed along another route.

I hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742420780a4440636.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信