How to export data to CSV file – Java
CSV is stand for Comma-separated values, CSV is a delimited data format that has fields/columns separated by the comma character and records/rows separated by newlines.
More detail please check http://en.wikipedia.org/wiki/Comma-separated_values
Please take a look at a CSV sample,
Display Name, Age, Hand Phone Micheal , 30, 0123456789 Bill, 25, 0129876543
Actually CSV is just like a text file with a certain delimited like comma “,” or semi-comma “;” or any other delimited character you want. Here i will show how to use java to export data or writing data into a CSV file.
This sample is very simple and straight froward, it just use java FileWriter object to create a normal text file (CSV file).
package com.mkyong.test; import java.io.FileWriter; import java.io.IOException; public class GenerateCsv { public static void main(String [] args) { generateCsvFile("c:\\test.csv"); } private static void generateCsvFile(String sFileName) { try { FileWriter writer = new FileWriter(sFileName); writer.append("DisplayName"); writer.append(','); writer.append("Age"); writer.append('\n'); writer.append("MKYONG"); writer.append(','); writer.append("26"); writer.append('\n'); writer.append("YOUR NAME"); writer.append(','); writer.append("29"); writer.append('\n'); //generate whatever data you want writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } }
Simple right? Writing or export data to a CSV file is exactly same with writing data into a normal text file. This is simple, we do not need any third party library to do it.
Thanks. It was helpful.
Hello sir; is there a way to export a txt file right into a csv with a program? Can’t be done manually because list is too long. Thanks for the consideration
Just write a simple program like what mention in above article.
The example of yours is converting hardcoded values to .csv file.
In my case i dont know the schema of the table..all i know is the name of the table and wer it resides,So can u help me in finding the best possible way to get the csv file in the below format;
tableName:ColName1,ColName2…ColName’n’
colElement1,colElement2,…,colElement’n’
.
.
.
[Till the last row.]
Please reply!!!
how does create new worksheet in csv by using Java????
csv is just a normal text file, see http://en.wikipedia.org/wiki/Comma-separated_values
[...] Re: Help with a beginner's java assignment: Survey results i've never used CSV, but I searched to how export data to a CSV file in Java. this link looks promising: http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/ [...]
And if you have a double quote in one of your fields? This is fairly simple to deal with but it should be mentionned. Replace all double quotes in any field with two double quotes, and enclose the whole in double quotes. The safe thing to do is always write one’s fields out enclosed by double quotes.
how can i generate csv file using junit api testing
Could you be more specified? In JUnit, just create a fucntion and generate the csv file, why not?
How can we embed an image in the CSV file??? Please help
er…may i know why you want to embed an image in CSV file?
Hi mkyong….

Srry it was simple…
I did cast it to string and use it
But however i could not append the time stamp(date + time) value. The timestamp value format is of YYYY:MM:DD HRS:MIN …..
But when appending it is only giving HRS:MIN value i.e the time value but the date value is completely lost
would you mind to share your code here? or email me?
What if i want to append double and integer values to the same files?
file writer append doesnt support double or int values….
>>> Simple right? Writing / export data to CSV file is exactly same with writing data into a normal text file.
Wrong. What if you have “,” in your, say, Display Name field?
hi no name,
Ya, i agreed with you, my function above is quite simple and it is not enough to cover all CVS functionality. However what i want to bring out is, we can generate the CVS file by simple output it as text file.
If we have a comma(,) , we need to use double quote (“) to enclose it.
For example
“No , name”, 23
Thanks for bringing this to my attention,
P.S Please refer to this link for CVS detail
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm