• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to saving data in csv file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have problem with saving data in csv file. I would like save my data look
like this example :

excel preview :

A B C
1 10 11 12
2 13 14 15



As we see all values are in separate cell A1=10, B1=11, C1=12 ...

so I try :

PrintWriter wy = new PrintWriter(new FileWriter("test.csv"));

values[0][0]="10";
values[0][1]="11";
values[0][2]="12";

values[1][0]="13";
values[1][1]="14";
values[1][2]="15";

for (String[] row : values){
for (String col : row) {
wy.print(col + "\t");
}
}

but csv file look like :

A1=10 11 12
A2=13 14 15

but B1-B2 and C1-C2 is empty

the second steep is use Ostermiller library :


OutputStream out;
out = new FileOutputStream("temp.csv");
CSVPrinter csvp = new CSVPrinter(out);

String[][] values = new String[2][3];

csvp.changeDelimiter('\t');

values[0][0]="10";
values[0][1]="11";
values[0][2]="12";

values[1][0]="13";
values[1][1]="14";
values[1][2]="15";

csvp.println(values);


but the result is also this same, is anyone do how to resolve this problem
?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The "C" in CSV is for comma. If you name a file ".csv", you should expect Excel to treat it as such, which means to use the comma as delimiter. Thus it is correct that all content is in the A column (because there is no delimiter). If you use a comma instead of a tab, everything should be fine. Alternatively, use Excels import function where you can tell it to use tabs as delimiters.
[ May 09, 2007: Message edited by: Ulf Dittmer ]
 
Tom Kulej
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can`t use excel to converting everything must be done in java programm
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just write the commas from Java:

System.out.println( array[0][0] + "," + array[0][1] + "," etc ... );

As long as you're only writing numbers, it's easy to put in the commas and Excel should read them just fine. Actually tabs should have worked just as well or better. I do this in another language and Excel runs through a little dialog while opening the file to ask about the delimiter.

System.out.println( array[0][0] + "\t" + array[0][1] + "\t" etc ... );

What extension do you use on your output? Any chance you opened the text file in an editor that removed the tabs or something?
 
Tom Kulej
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank`s for reply and help....

Why I must write a value to seperate cell i csv file ? The answer is simple.. I use ccapi library to count same indicators and this library use this "special" csv file format (all values are in seperate cell`s)....

So the first step is use poi library to write excel file... all values I write to seperate cells but the library ccapi don`t work with excel file only with csv file (right format).

Write with comma don`t resolve my problem... What can I get ? open a simple excel file and do file->save as csv file... and we see that what I`am thinking all values are in seperate cell... and the file is csv...
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm confused. If a library is used to access those files, then what does it matter whether or not Excel thinks that all values go into one cell? The important thing is what the library does.
 
reply
    Bookmark Topic Watch Topic
  • New Topic