gpt4 book ai didi

json - 如何将 json 对象转换为 .csv 文件以及如何使用 Ionic 2 + angular 2 导出?

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

我在 angular 2 应用程序中使用一个函数将 JSON 数据转换并导出为 .CSV。以下是我的功能,在 angular 2 应用程序中运行良好
(网络)。与我尝试在使用 Ionic 2 开发的移动应用程序中使用的相同,它在移动应用程序中不起作用。有没有办法做到这一点?

谢谢!

saveAsCSV() {
let sampleJson : any = [{name:'ganesh', age:'24'},{name:'ramesh', age:'24'},{name:'suresh', age:'24'}]
this.saveData = [];
let a = document.createElement("a");
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);
let csvData = ConvertToCSV(sampleJson);
let blob = new Blob([csvData], { type: 'text/csv' });
let url= window.URL.createObjectURL(blob);
a.href = url;
a.download = 'sample.csv';
a.click();
}

ConvertToCSV(objArray) {
let array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
let str = '';
let row = "";
for (let index in objArray[0]) {
//Now convert each value to string and comma-separated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
str += row + '\r\n';

for (let i = 0; i < array.length; i++) {
let line = '';
for (let index in array[i]) {
if (line != '') line += ',';
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}

最佳答案

使用“cordova-plugin-file”在 Ionic 2 中导出 csv。

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/

如果您看到以下错误 -

代码:1 消息:“NOT_FOUND_ERR”

在 config.xml 中添加以下行之一来解决它。

<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />

如插件文档中所述,您可以使用这两个选项之一:

choose whether to store files in the internal file storage location, or using the previous logic, with a preference in your application's config.xml file. Without this line, the File plugin will use Internal as the default. If a preference tag is present, and is not one of these values, the application will not start.

If your application has previously been shipped to users, using an older (pre- 3.0.0) version of this plugin, and has stored files in the persistent filesystem, then you should set the preference to Compatibility if your config.xml does not specify a location for the persistent filesystem. Switching the location to "Internal" would mean that existing users who upgrade their application may be unable to access their previously-stored files, depending on their device.

If your application is new, or has never previously stored files in the persistent filesystem, then the Internal setting is generally recommended.

关于json - 如何将 json 对象转换为 .csv 文件以及如何使用 Ionic 2 + angular 2 导出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897720/

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