最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add data to new column in csv file with Javascript - Stack Overflow

programmeradmin5浏览0评论

I want add data to new column in csv file. I using Javascript. I try with '\r\n' but not work. Comma make new row in same column.

for (var i = 0, l = mydata.length; i < l; ++i) {
    csvRows.push(mydata[i].join(','));
}
csvRows.push('\r\n'); //here want to go in second column
csvRows.push(mydata[0].join(',')); //this want write in second column first row
var csvString = csvRows.join("%0A");
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvString;
a.target = '_blank';
a.download = 'file.csv';
document.body.appendChild(a);
a.click();

What I must push in csvRows to make new column? FIDDLE is similar example.

I want add data to new column in csv file. I using Javascript. I try with '\r\n' but not work. Comma make new row in same column.

for (var i = 0, l = mydata.length; i < l; ++i) {
    csvRows.push(mydata[i].join(','));
}
csvRows.push('\r\n'); //here want to go in second column
csvRows.push(mydata[0].join(',')); //this want write in second column first row
var csvString = csvRows.join("%0A");
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvString;
a.target = '_blank';
a.download = 'file.csv';
document.body.appendChild(a);
a.click();

What I must push in csvRows to make new column? FIDDLE is similar example.

Share Improve this question edited Aug 9, 2017 at 9:09 Nejc Galof asked Jul 7, 2016 at 12:51 Nejc GalofNejc Galof 2,6243 gold badges36 silver badges72 bronze badges 6
  • Looks like you need to loop through every thing in the array and add , . You just can't push something here. – Praveen Kumar Purushothaman Commented Jul 7, 2016 at 12:51
  • And please plete all the inplete codes! csvRows, mydata, l, etc. – Praveen Kumar Purushothaman Commented Jul 7, 2016 at 12:53
  • @PraveenKumar I update question. In fiddle is similar example how it's work. – Nejc Galof Commented Jul 7, 2016 at 12:55
  • Check my answer. Gives you some idea. – Praveen Kumar Purushothaman Commented Jul 7, 2016 at 12:55
  • I have updated my answer. – Praveen Kumar Purushothaman Commented Jul 7, 2016 at 12:59
 |  Show 1 more ment

2 Answers 2

Reset to default 4

Problem is specific when I open with Microsoft Excell. When I try with other programs, this works.

In Microsoft Excell work for me, if I change , to ;

Let's try here:

var data = [
  "Col1,Col2,Col3",
  "Col4,Col5,Col6"
];
console.log(data);

But if you wanna add one more column, you need:

var data = [
  "Col1,Col2,Col3",
  "Col4,Col5,Col6"
];
for (var i = 0; i < data.length; i++)
  data[i] += ",NewCol" + (i + 1);
console.log(data);

发布评论

评论列表(0)

  1. 暂无评论