Javascript/jQuery:以CSV格式导出数据无法在IE中运行

Shu*_*ubh 19 javascript export-to-csv

我需要将表格中显示的数据导出为CSV格式.我已经尝试了很多东西,但无法让它适用于IE 9及更高版本.

我用我的代码创建了一个虚拟小提琴.

var data = [
    ["name1", "city1", "some other info"],
    ["name2", "city2", "more info"]
];//Some dummy data

var csv = ConvertToCSV(data);//Convert it to CSV format
var fileName = "test";//Name the file- which will be dynamic

if (navigator.userAgent.search("MSIE") >= 0) {
    //This peice of code is not working in IE, we will working on this
    //TODO
    var uriContent = "data:application/octet-stream;filename=" + fileName + '.csv' + "," + escape(csv);
    window.open(uriContent + fileName + '.csv');
} else {
    var uri = 'data:text/csv;charset=utf-8,' + escape(csv);
    var downloadLink = document.createElement("a");
    downloadLink.href = uri;
    downloadLink.download = fileName + ".csv";
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink);
}
Run Code Online (Sandbox Code Playgroud)

我在Stackoverflow中看到了很多链接,但是找不到任何与IE9或更高版本一起使用的链接.就像@ Terry Young在如何使用数据导出到csv-using-jquery-or-javascript中解释的那样

还试过 -

var csv = ConvertToCSV(_tempObj);
        var fileName = csvExportFileName();
        if (navigator.appName != 'Microsoft Internet Explorer') {
            window.open('data:text/csv;charset=utf-8,' + escape(str));
        }
        else {
            var popup = window.open('', 'csv', '');
            popup.document.body.innerHTML = '<pre>' + str + '</pre>';
        }
Run Code Online (Sandbox Code Playgroud)

不知道如何解决它.我不想点击服务器并导出我的CSV(要求这样说).

ina*_*ain 17

使用Javascript后,它将解决您的问题.

用于IE,

var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
Run Code Online (Sandbox Code Playgroud)

有关更多信息,我已经编写了相关的教程,请参阅 - 以CSV格式下载跨浏览器支持的JSON数据

希望这对你有所帮助.

  • 非常感谢你的解决方案.这帮助了我.此解决方案也可用于导出XLS(Excel)文件.要导出excel文件,请用包含excel文件的xml定义的变量替换"CSV"行,然后将".csv"扩展名替换为".xls". (2认同)

小智 7

对于IE 10+,您可以:

var a = document.createElement('a');
        if(window.navigator.msSaveOrOpenBlob){
            var fileData = str;
            blobObject = new Blob([str]);
            a.onclick=function(){
                window.navigator.msSaveOrOpenBlob(blobObject, 'MyFile.csv');
            }
        }
        a.appendChild(document.createTextNode('Click to Download'));
        document.body.appendChild(a);
Run Code Online (Sandbox Code Playgroud)

我认为在早期版本的IE中不可能.不调用activeX对象,但如果可以接受,则可以使用:

var sfo=new ActiveXObject('scripting.FileSystemObject');
var fLoc=sfo.CreateTextFile('MyFile.csv');
fLoc.WriteLine(str);
fLoc.close();
Run Code Online (Sandbox Code Playgroud)

哪个会将文件直接写入用户的文件系统.但是,这通常会提示用户询问是否要允许脚本运行.可以在Intranet环境中禁用该提示.


小智 5

这也是我使用的答案之一,并且在IE 10+版本中效果很好:

var csv = JSON2CSV(json_obj);            
var blob = new Blob([csv],{type: "text/csv;charset=utf-8;"});

if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, "fileName.csv")
    } else {
        var link = document.createElement("a");
        if (link.download !== undefined) { // feature detection
            // Browsers that support HTML5 download attribute
            var url = URL.createObjectURL(blob);
            link.setAttribute("href", url);
            link.setAttribute("download", "fileName.csv");
            link.style = "visibility:hidden";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }           
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。