Search code examples
javacsvapache-commons-csv

Java commons-csv doesn't parse header


I have an EXCEL CSV file with this content:

access_id;logicalgate
123456789;TEST

As you can see header has strings and are not quoted.

With this code:

fileReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
csvParser = new CSVParser(fileReader, CSVFormat.EXCEL.withNullString("").withFirstRecordAsHeader().withIgnoreHeaderCase().withQuoteMode(QuoteMode.MINIMAL).withIgnoreEmptyLines().withTrim());

The output of this command:

csvParser.getHeaderMap()
    

Is a single element map!

(java.util.TreeMap<K,V>) {access_id;logicalgate=0}

With a concatendated key "access_id;logicalgate"

Why the parser is missing the header separation?


Solution

  • CsvFormat.EXCEL is defined with delimiter , not ;, you should add .withDelimiter(';').

    public static final CSVFormat EXCEL

    Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is locale dependent, it might be necessary to customize this format to accommodate to your regional settings.

    For example for parsing or generating a CSV file on a French system the following format will be used:

    CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');

    The CSVFormat.Builder settings are:

    setDelimiter(',')
    setQuote('"')
    setRecordSeparator("\r\n")
    setIgnoreEmptyLines(false)
    setAllowMissingColumnNames(true)
    setAllowDuplicateHeaderNames(true)
    

    -- https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#EXCEL