aspose file tools*
The moose likes Other Open Source Projects and the fly likes Write Bean values to a CSV file using OpenCSV Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Write Bean values to a CSV file using OpenCSV" Watch "Write Bean values to a CSV file using OpenCSV" New topic
Author

Write Bean values to a CSV file using OpenCSV

Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
I am using opencsv to read a CSV file and it works well.
I am currently trying to retrieve details from a database using hibernate and want to write the values to a csv file.

I saw they have examples where you can use the column mapping strategy and tie the indexes of the csv to a java bean but opencsv doesnt have anything like that for writing to csv files.
Lets say I get a)a list of Employee objects b)a list of Employee objects with Address (another class, included as a java.util.set in the employee pojo)embedded within
How could I go about writing a csv file in this scenaro ?

Cheers,
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2492
    
    8

There's a BeanToCsv class in the OpenCSV API. Well you already figured out how to read bean to csv like:


Then to write


I figured you wanted to write a list of list of beans then first how to produce a list of list of beans from reading then loop through it while writing.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
Thanks for your response. Sadly, I wasn't able to figure out a column mapping strategy while reading from csv.
How could one write directly from a bean, when it is a simple pojo and also when it has nested pojos within itself ?
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
K. Tsang wrote:There's a BeanToCsv class in the OpenCSV API. Well you already figured out how to read bean to csv like:


Then to write


BTW, BeanToCsv is for ver 2.4 which isnt released yet.
I figured you wanted to write a list of list of beans then first how to produce a list of list of beans from reading then loop through it while writing.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2492
    
    8

To produce list of list of beans List<List<YourBean>> is fairly simple if you got already got the original list.

Do the tradition stuff to populate the internal list as before.


Since BeanToCsv is not yet available I suggest you manually writing a class or function to does the same purpose using java.io package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write Bean values to a CSV file using OpenCSV