File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Creating a CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a CSV file" Watch "Creating a CSV file" New topic
Author

Creating a CSV file

sonu rai
Greenhorn

Joined: May 23, 2007
Posts: 22
Hi All,

I want to create a csv file , I have to give heading and beside it there will be detail of that heading. such as Name is an heading and bellow it name of students will be. So, what can i do.

Thank's
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Writing CSV files is very easy. "CSV" means "comma separated values". A CSV file consist of records; each line is a record, and the fields in each record are separated by commas.

You just write a program that opens the file for writing, first write the header line, then loop through the records, for each record write a line with the data for that record (fields separated by spaces), then close the file.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39541
    
  27
CSV does not have a concept of headers. It has rows and columns of values. ANy program processing a CSV file is of course free to treat certain rows or cells in special ways (as if they were headers).

It gets a little more complicated that Jesper described, though. Sometimes the delimiter is not a comma but a semicolon, and if the delimiter occurs inside of a cell, then the cell contents need to be enclosed in double quotes. And if the contents contain double quotes or newline characters, then those need to be escaped.

Before you've coded up all these special cases, you might as well use a ready-made library for reading and writing CSVs. The AccessingFileFormats FAQ page lists several such libraries.


Ping & DNS - updated with new look and Ping home screen widget
poorna prakash parvathala
Greenhorn

Joined: Dec 18, 2006
Posts: 20
try this

http://ostermiller.org/utils/CSV.html

it has the utility programs you can extract to suit your needs


Poorna Prakash<br /> <br />SCJP 1.4 (90%), SCWCD (planning)
 
 
subject: Creating a CSV file
 
Similar Threads
CSV File
Approach to validate objects in list
Struts FormFile (parsing .csv files)
java code to export mssql data into csv file
UTF date formatting