Perhaps I am not understanding what the method captureHeader() in OpenCSV is for, but the method grabs a CSV file's headers and sets a protected 'header' variable to a String array of those values.
But then how do you access those header values to try and match say "first_name" from the csv to "firstName" in your Bean? The idea being to create the MappingStrategy from those headers before actually parsing the full csv file. However captureHeader() is a void and 'header' is protected?
OpenCSV API
You need to implement the captureHeader method and store the string[] header in a list. You can now iterate through this list and map according to your bean.
Example:
public void captureHeader(CSVReader reader){
super.captureHeader(reader);
List<String> csvHeader = Arrays.asList(header);
//iterate through the list
}
You could also use HeaderColumnNameTranslateMappingStrategy for your task. Refer to my answer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With