Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SuperCSV to Change Header Values

Tags:

java

supercsv

In line with this post about abilities of SuperCSV, can SuperCSV handle changing the values of the headers(column names only) read from the database?

For example, the following code snippet details the Current State and the Expected State.

Current State

INPUT:

final String[] header = new String[] { "firstName", "lastName", "birthDate"};

// write the header
beanWriter.writeHeader(header);

// write the beans
for( final CustomerBean customer : customers ) {
   beanWriter.write(customer, header, processors);
}

OUTPUT: file with column names:

firstName, lastName, birthDate
Bob      , Doe     , 02/12/2013  

Expected State

INPUT:

final String[] header = new String[] { "firstName", "lastName", "birthDate"};

// write the header
beanWriter.writeHeader(header);

// write the beans
for( final CustomerBean customer : customers ) {
   beanWriter.write(customer, header, processors);
}

// modify the headers
 ??????

OUTPUT: file with modified column names:

First Name, Last Name, Birthday
Bob      , Doe     , 02/12/2013 

Any assistance is greatly appreciated.

like image 361
todun Avatar asked Feb 21 '14 18:02

todun


1 Answers

I'm not sure if this answers your question, but you can put whatever you like in the header - it doesn't have to be identical to the mapping array passed to beanWriter.write()

For example, the following will give the output you desire:

final String[] header = new String[] { "First Name", "Last Name", "Birthday"};
final String[] fieldMapping = new String[] { "firstName", "lastName", "birthDate"};

// write the header
beanWriter.writeHeader(header);

// write the beans
for( final CustomerBean customer : customers ) {
   beanWriter.write(customer, fieldMapping , processors);
}
like image 79
James Bassett Avatar answered Sep 21 '22 14:09

James Bassett