Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using captureHeader in OpenCSV

Tags:

java

opencsv

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

like image 562
xref Avatar asked Jun 17 '26 02:06

xref


1 Answers

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.

like image 103
baskar_p Avatar answered Jun 19 '26 16:06

baskar_p



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!