使用SuperCSV更改标头值

时间:2014-02-21 18:12:04

标签: java supercsv

根据这篇关于abilities of SuperCSV的帖子,SuperCSV可以处理从数据库中读取的标题(仅 列名 )的值吗?

例如,以下代码段详细说明了当前状态和预期状态。

当前状态

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 :包含列名称的文件:

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

预期状态

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 :包含已修改列名称的文件:

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:11)

我不确定这是否能回答您的问题,但您可以在标题中添加您喜欢的内容 - 它不必与传递给beanWriter.write()的映射数组相同

例如,以下内容将提供您想要的输出:

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);
}