Javascript / jQuery:以CSV格式导出数据无法在IE中运行 [英] Javascript/ jQuery : Exporting data in CSV not working in IE
问题描述
我需要将表格中显示的数据导出为CSV格式。我已经尝试了很多东西但是无法让它适用于IE 9及更高版本。
I need to Export Data displayed in a Table to CSV Format. I have tried lot many things but couldn't get it working for IE 9 and above.
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);
}
我在Stackoverflow中看到很多链接,但找不到任何东西这与IE9或更高版本一起使用。就像 @ Terry Young解释如何使用数据 - export-to-csv-using-jquery-or-javascript
I have seen many of the links in Stackoverflow, but couldn't find anything that's working with IE9 or above. Like @ Terry Young explains in how-to-data-export-to-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>';
}
不确定如何修复它。我不想点击服务器并导出我的CSV(要求这样说)。
Not sure how to fix it. I don't want to hit the server and export my CSV (the requirement say so).
推荐答案
使用Javascript后它会解决你的问题。
After using Javascript it will solve your problem.
用于IE,
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
有关详细信息,我已经编写了相关的教程,请参阅 - 以CSV格式下载JSON数据跨浏览器支持
For more information i have written tutorial on that, see - Download JSON data in CSV format Cross Browser Support
希望这会对你有所帮助。
Hope this will be helpful for you.
这篇关于Javascript / jQuery:以CSV格式导出数据无法在IE中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!