6

根据这篇关于SuperCSV 能力的帖子,SuperCSV可以处理更改从数据库读取的标题(仅限列名)的值吗?

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

当前状态

输入

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

输出:具有列名的文件:

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

预期状态

输入

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 

非常感谢任何帮助。

4

1 回答 1

13

我不确定这是否回答了您的问题,但您可以在标题中放置您喜欢的任何内容 - 它不必与传递给的映射数组相同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);
}
于 2014-02-22T09:44:00.477 回答