导出表格到MS Excel的JavaScript,MS边缘不工作

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

问题来了,当我在全新的MS Edge网页浏览器上进行testing时,会打开一个新的空白标签。 没有控制台错误,没有警告popup,什么都没有。 正如我所说,在这个链接有办法处理出口到Excel中的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" ); } }()); 

在线testing: http : //jsfiddle.net/jonathansampson/nc4k4hz8/

您需要执行一些function检测以查看msSaveBlobmsSaveOrOpenBlob是否可用。 如果他们是,使用它们,如果他们不是,你可以沿着另一条路线前进。

我希望这有帮助。