1

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?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • 1
    I can't see anywhere where you specify that a semicolon is the delimiter. How's it supposed to know? Needs `.withDelimiter(';')`, surely? I imagine the rows themselves are also broken. – Michael Jul 19 '22 at 15:33
  • 1
    the [CSVFormat.EXCEL](https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#EXCEL) specifies the delimiter, just the wrong one (`,`) in this case. – criztovyl Jul 19 '22 at 15:51
  • Incredible... thanks for the tip! – Tobia Jul 19 '22 at 15:54

1 Answers1

2

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

criztovyl
  • 761
  • 5
  • 21