0

I would like to write a string in he columns of a csv file using javascript. I'm using the below code, but it writes the entire string in 1 column.

var header="Number" + ',' + "Name" + ',' + "Description"  +   '\n' ;

this is how I create the csv fie:

    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var s = fso.CreateTextFile("C:\\REPORT.csv");

Any suggestions?

GBI
  • 27
  • 1
  • 2
  • 8
  • How exactly are you creating the CSV file itself? Also, you could just put all of that in one string... `header = "Number","Name","Description"\n` – Christian Aug 19 '15 at 08:39
  • i updated my question, also I changed the string as per your sugestion, but didnt help in splitting into different column. I also opened in excel, but all was writen in 1 column. – GBI Aug 19 '15 at 08:48
  • http://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side – Shebin Koshy Aug 19 '15 at 09:37

2 Answers2

1

There's nothing wrong with your code. There are few CSV standards which can use different character for separator, e.g. USA/UK CSV uses comma for separator, while European CSV/DSV uses semicolon for separator.

You just should make sure that your viewer is properly configured, in OpenOffice Calc when you try to open CSV, you see the following window prompt where you can select the right separator.


If you're using Excel: How to correctly display .csv files within Excel 2013?

Community
  • 1
  • 1
kav
  • 5,363
  • 6
  • 28
  • 53
  • Thanks a lot Unixarmy. It was the wrong separator. after using ";", i got the desired output. Is it possible to specify the cell width to fit the long string. – GBI Aug 19 '15 at 08:58
  • You can't do this using CSV. – kav Aug 19 '15 at 09:01
0

There's also an option in Windows to control the separator :

Go to "Control Panel"/"Region and Language"/"Change the date, time or number format". In the "Region and Language" window that opens, click "Additional settings..." in the "Format" Tab.

On the "numbers" tab, there is a "list separator" which you can choose between semicolon (;) and comma (,). When applying the changes and opening the file with excel again, it will be all good.

The only problem with that solution is it changes the entire system separator. So if you have other files with semicolon separator you want to open in excel, then you will have to import it as text file and change some parameters...

Grom
  • 3
  • 1