• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Write Bean values to a CSV file using OpenCSV

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Danger, 10,000 volts, very electic .... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic