如何使用BeanParser读取具有重复列名的csv文件。
以下是示例标题
Col desc,Col amount,Col desc,Col amount
test,12.44,test2,43.44
答案 0 :(得分:0)
您可以手动设置标题名称:
public class MyClass {
@Parsed(field = "amount1")
private BigDecimal amount1;
@Parsed(field = "amount2")
private BigDecimal amount2;
@Parsed(field = "description1")
private String description1;
@Parsed(field = "description2")
private BigDecimal description2;
}
然后:
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setHeaderExtractionEnabled(true); //to read your input headers with duplicates.
parserSettings.setHeaders("description1", "amount1, "description2", "amount2"); //to override the headers in the input with the names you want to work with
然后解析:
List<MyClass> myList = new CsvRoutines(parserSettings).parseAll(MyClass.class, <input>);
您还可以使用字段索引而不是注释中的字段名称,因此您不必担心标题名称。
希望这有帮助