我正在尝试将我的数据导出为CSV文件。我写了下面的代码,这在Firefox / Chrome中运行良好,但在IE中却没有。我需要在IE8 / 9/10版本中工作。提前谢谢。
JS代码:
var CSVgen = function (CSV, ReportName) {
//Generate a file name if empty is replace by _.
var fileName = ReportName.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;Content-Type:application/octet-stream;Content-Disposition: attachment;charset=utf-8,' + escape(CSV);
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
我也试过以下方式:
var uri ='data:text / csv; charset = utf-8,'+ escape(CSV);
和
var uri ='Content-Type:application / octet-stream; Content-Disposition: 附件;' + escape(CSV);
但没有运气。我在IE中的错误页面下面:
The webpage cannot be displayed
Most likely cause:
•Some content or files on this webpage require a program that you don't have installed.
What you can try:
Search online for a program you can use to view this web content.
Retype the address.
Go back to the previous page.