[英]Export JavaScript Array of Filtered HTML Table data to MS Excel or CSV
[英]Export table to MS Excel with Javascript, on MS Edge not working
为了安全起见 ,您目前不能在Internet Explorer或Microsoft Edge中导航到数据URL。 但是,可以使用msSaveBlob
或msSaveOrOpenBlob
下载或保存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/
您将需要执行一些功能检测,以查看msSaveBlob
或msSaveOrOpenBlob
是否可用。 如果是这样,请使用它们;如果不是,则可以采用其他方法。
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.