0

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.

Dhaval Patel
  • 141
  • 1
  • 9

1 Answers1

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 :)

Yann39
  • 14,285
  • 11
  • 56
  • 84
Scott Conway
  • 975
  • 7
  • 13
  • Thanks for the answer. – Dhaval Patel Aug 21 '15 at 08:27
  • Still I need some favor from you, as it seems the in the solution provided by you in comment is creating whole list to be written to the CSV file at once. But if my data is too large than it may create problem. Is there any solution for it. I may misunderstood your answer even till now i didn't tried the solution provided by you but this is just based on my overview. – Dhaval Patel Aug 21 '15 at 08:30
  • 1
    Ahh - in that case the CSVWriter has a writeNext method that you just pass in an array of Strings that is one line of the csv file you are writing. In that case in the directions above replace the list instructions in 2 & 3 with the call to writeNext and you do not need step 4. Hope that helps :) – Scott Conway Aug 21 '15 at 11:34