繁体   English   中英

在MS Edge上无法使用Java表格将表导出到MS Excel

[英]Export table to MS Excel with Javascript, on MS Edge not working

我一直使用此链接中显示的相同代码将表格从HTML导出到Excel,并且在许多浏览器中都可以很好地工作。

问题是当我测试它的全新MS边缘网络浏览器,它开辟了一个新的空白标签,这一点。 没有控制台错误,没有警告弹出窗口,什么都没有。 正如我所说,在该链接中,有一种方法可以处理IE中向Excel的导出。

因此,我想知道是否有人知道支持新的Microsoft Edge浏览器的类似技巧。

谢谢。

为了安全起见 ,您目前不能在Internet Explorer或Microsoft Edge中导航到数据URL。 但是,可以使用msSaveBlobmsSaveOrOpenBlob下载或保存msSaveOrOpenBlob

我在下面为您准备了一个基本示例:

(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" );

  }

}());

在线测试: http//jsfiddle.net/jonathansampson/nc4k4hz8/

您将需要执行一些功能检测,以查看msSaveBlobmsSaveOrOpenBlob是否可用。 如果是这样,请使用它们;如果不是,则可以采用其他方法。

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM