'How to Write/Generate CSV file from annotated java class

I have a objects filled with data. Now I want to create class with annotation that allows me to create CSV file with specified order of columns or any generic solution for it. My data representation is somewhat complex means its with list of objects.

For Example:

class User {
public String name;
public int age;

List<Relatives> relativeList;
public User() {
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

public List<Relatives> getRelativeList() {
    return relativeList;
}

public void setRelativeList(List<Relatives> relativeList) {
    this.relativeList = relativeList;
}
}

class Relatives {

public int id;
public String name;

public void setId(int id) {
    this.id = id;
}

public int getId() {
    return id;
}

public void setName(String name) {
    this.name= name;
}

public String getName() {
    return name;
}
}

I want to create CSV file out of data filled in objects of this class.

For Example

User user = new User();
user.setName("Jackson");
user.setAge(32);
List<Relatives> relativeList = new ArrayList<Relatives>();
Relatives relatives = new Relatives();
relatives.setId(110);
relatives.setName("father");
relativeList.add(relatives);

relatives = new Relatives();
relatives.setId(111);
relativ.add(relatives);
relativeList.setName("mother");

user.setRelativeList(relativeList);

I want to create CSV text like.

"name";"age";"relatives id1";"relatives name1","relatives id2";"relatives name2"
"Jackson";"30";"110";"father";"111";"mother"

As described by Example, If i have 3 elements in relativeList than one more column with name "relatives id3" and "relatives name3" should be added. Which is element + sub-element means relatives + (id or name). Along with this i want to decide the order of columns in generated CSV means age can be settled as a first column before name.

It may possible that exact solution for this is not readily available but any near by solution is really appreciated and will be helpful.



Solution 1:[1]

It's the List within the object that is the killer. OpenCSV has a BeanToCsv class to convert a list of objects into a CSV file but I doubt it works with collections (never tried it though).

You can still use it if you want to limit the number of relatives a person has. Then you create an intermediate class that has the raw data fields with a constructor that takes your User class. Then using the BeanToCsv create the csv output you want.

However it sounds like you don't want to limit the user's number of relatives. So you can use openCSV to roll your own. Given a list of users objects you will have to do four things:

  1. Determine the largest number of relatives the user has in your list.
  2. Create a String array with the header values using the value from #1 to determine how many relative name and id columns to put. Once that is done create a List<String []> and put the array in the list.
  3. Create a helper class to convert a User into a String array. Pass in the value from #1 so if you have fewer relatives than max you can pad with empty strings or null (your choice). Using this loop through the list of user objects converting each user to an array of strings and add that array to the the list created in #2.
  4. Using the CSVWriter in openCsv call the writeAll method passing in the list you created and populated in steps 2 and 3.

Good luck :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yann39