admin管理员组文章数量:1414736
In an angular project im trying to implement a function to generate a CSV file from an array of objects.
The array of objects populates a ng-Table so at first i tried to use: This worked great in chrome, in IE it doesnt work at all though because it needs to use the download attribute...
Then i tried this approach
var objArray = [
{ name: "Item 1", color: "Green", size: "X-Large" },
{ name: "Item 2", color: "Green", size: "X-Large" },
{ name: "Item 3", color: "Green", size: "X-Large" }];
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
But, now all the data of a row is in one single column. I want need the data on seperate columns.
Expected output (once opened in Excel)
Anyone got an idea how to do this?
In an angular project im trying to implement a function to generate a CSV file from an array of objects.
The array of objects populates a ng-Table so at first i tried to use: http://bazalt-cms./ng-table/example/15 This worked great in chrome, in IE it doesnt work at all though because it needs to use the download attribute...
Then i tried this approach
var objArray = [
{ name: "Item 1", color: "Green", size: "X-Large" },
{ name: "Item 2", color: "Green", size: "X-Large" },
{ name: "Item 3", color: "Green", size: "X-Large" }];
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
But, now all the data of a row is in one single column. I want need the data on seperate columns.
Expected output (once opened in Excel)
Anyone got an idea how to do this?
Share Improve this question edited Mar 13, 2014 at 15:21 Fredkr asked Mar 13, 2014 at 14:49 FredkrFredkr 7233 gold badges8 silver badges21 bronze badges 10- What's the purpose of converting to JSON and then back again? – Pointy Commented Mar 13, 2014 at 14:51
- Well that code works just fine, as far as I can tell. Be warned that you (strictly speaking) can't rely on the order of properties in objects. Depending on the "life history" of each object in the array, you won't necessarily get the properties in the same order ("name", "color", "size" could be shuffled, in other words). – Pointy Commented Mar 13, 2014 at 15:08
- It creates a csv, thats not the problem. The problem is the format of the outputed csv. I need name to be in one column, color in a second and size in a third. Atm they are all in the same column – Fredkr Commented Mar 13, 2014 at 15:12
- Not when I run it; there are mas between them. Maybe I don't know what you mean by "column". What are you doing with the string after that bit of code? – Pointy Commented Mar 13, 2014 at 15:14
- 1 You should post your expected output. The output of this JSFiddle looks proper csv – shyam Commented Mar 13, 2014 at 15:15
1 Answer
Reset to default 3This looks like an Excel question.
But, now all the data of a row is in one single column. I want need the data on seperate columns... (once opened in Excel)
You are creating the file like this: Convert JSON format to CSV format for MS Excel
The problem is that your file doesn't have a csv extension. Instead of double clicking the file or allowing IE to automatically open it, open Excel and choose File > Open. Browse to the file and click Open. This will invoke the Text Import wizard where you can specify ma as the delimeter.
Update
You can tell Excel how to process the file by adding "sep=," to the first line. In your code, add the following line after your loop:
str = 'sep=,\r\n' + str;
If you open the file in Notepad, it will look like this:
sep=,
name,color,size
Item 1,Green,X-Large
Item 2,Green,X-Large
Item 3,Green,X-Large
However, when the file is opened in Excel, only the headers and data will be present:
Here is a demo, forked from Joseph Sturtevant's fiddle posted in the answers to the related question: http://jsfiddle/wittwerj/6JySt/
本文标签: javascriptJSON to CSV with separated columnsStack Overflow
版权声明:本文标题:javascript - JSON to CSV with separated columns - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697584a2620373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论