gpt4 book ai didi

Javascript/jQuery : Exporting data in CSV not working in IE

转载 作者:行者123 更新时间:2023-12-03 08:03:47 25 4
gpt4 key购买 nike

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

我有 created a dummy fiddle用我的代码。

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 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(要求是这样的)。

最佳答案

使用 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();

有关我编写的教程的更多信息,请参阅 - Download JSON data in CSV format Cross Browser Support

希望这对您有所帮助。

关于Javascript/jQuery : Exporting data in CSV not working in IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18185660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com