我尝试使用以下代码进行导出,但如果json数据量过大,则会在Chrome浏览器中显示错误('失败 - 网络错误')。对于小尺寸数据,它工作正常。
<script>
var items = [
{university: 'A',college: 'X',state: 'J'},
{university: 'B',college: 'Y',state: 'K'},
{university: 'C',college: 'Z',state: 'L'}
];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject;
var csv = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
csv += line + '\r\n';
}
var uri = 'data:text/csv;charset=utf-8,' + csv;
data = encodeURI(uri);
var link = document.createElement("a");
link.href = data;
link.style = "visibility:hidden";
link.download = "xyz.csv";
link.click();
</script>
由于
答案 0 :(得分:0)
尝试此操作可获取大量数据
var items = [{
university: 'A',
college: 'X',
state: 'J'
},
{
university: 'B',
college: 'Y',
state: 'K'
},
{
university: 'C',
college: 'Z',
state: 'L'
}
];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject;
var csv = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
csv += line + '\r\n';
}
csvData = new Blob([csv], {
type: "data:application/vnd.ms-excel;charset=utf-8,\uFEFF"
});
var csvUrl = URL.createObjectURL(csvData);
var a = document.createElement("a");
a.href = csvUrl;
a.setAttribute("download", fileName + ".csv");
document.body.appendChild(a);
a.click();