【发布时间】:2015-03-27 07:42:04
【问题描述】:
Json 到 CSV 以下载在 chrome 中工作但在 IE 浏览器中不工作?任何机构都有解决方案 这。
Json 数据:
[
{
"id":1, "name":"Johnson, Smith, and Jones Co.",
"amount":345.33, "Remark":"Pays on time"
},
{
"id":2, "name":"Sam \"Mad Dog\" Smith",
"amount":993.44, "Remark":""
},
{
"id":3, "name":"Barney & Company",
"amount":0, "Remark":"Great to work with\nand always pays with cash."
},
{
"id":4, "name":"Johnson's Automotive",
"amount":2344, "Remark":""
}
]
功能:
JSONToCSVConvertor = function (JSONData, Filename, ColumnDataTypes, ShowLabel)
{
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var array = ColumnDataTypes.split(',');
var CSV = '';
var ColumnName = ColumnDataTypes.split(',');
if (ShowLabel) {
var row = "";
for (var x = 0; x < array.length ; x++) {
row += array[x].split('-')[1] + ',';
}
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (Col = 0; Col < array.length; Col++) {
row += ('"' + arrData[i][array[Col].split('-')[0]] + '",').replace('""', '"');
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = Filename;
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
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);
};
【问题讨论】:
-
在 IE 中会发生什么?是否抛出任何错误?
-
能否请您更新此代码JSFIDDLe(它应该适用于chrome)或者是否可以提供实时网站,我们可以在那里检查。
-
例外是“传递给系统调用的数据区域太小”。
标签: javascript jquery json internet-explorer-8 export-to-csv