javaapache-commons-csv

Text qualifier - invalid char between tokn encapsulated and delimiter


If there is a comma in the field, but the whole is closed with quotation marks, then I should not treat it as a column divider. How can this be done?

Example aaaa, "bb,bb", cccc and I get aaaa | bb | bb |ccc

How can I receive aaaa | "bb,bb" | cccc ?

public List<CSVRecord> collectAllEntries(Path path) throws IOException {
        logger.info("Parsing the input file" + path);
        List<CSVRecord> store = new ArrayList<>();
        try (
                Reader reader = Files.newBufferedReader(path, Charset.forName("ISO-8859-2"));
                CSVParser csvParser = new CSVParser(reader, CSVFormat.EXCEL.withQuote(';'))
        ) {
            for (CSVRecord csvRecord : csvParser) {
                store.add(csvRecord);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return store;
    }
private void csvToXlsx(Path csvFilePath, Path excelFilePath) throws Exception {
    logger.info("Converting CSV to XLSX" + excelFilePath);
    List<CSVRecord> records = collectAllEntries(csvFilePath);
    XSSFWorkbook myWorkBook = new XSSFWorkbook();
    FileOutputStream writer = new FileOutputStream(new File(excelFilePath.toString()));
    XSSFSheet mySheet = myWorkBook.createSheet();
    IntStream.range(0, records.size())
            .forEach(rowNum -> {
                XSSFRow myRow = mySheet.createRow(rowNum);
                CSVRecord record = records.get(rowNum);
                for (int i = 0; i < record.size(); i++) {
                    XSSFCell myCell = myRow.createCell(i);
                    myCell.setCellValue(record.get(i));
                }
            });
        myWorkBook.write(writer);
        writer.close();
    }

Solution

  •  private void processOrderSet(HashMap<String, List<CSVRecord>> entries, FileWriter out, List<String> headers) throws IOException {
            try (CSVPrinter printer = new CSVPrinter(out, CSVFormat.EXCEL.withHeader(headers.toArray(new String[0])).withQuote('"').withDelimiter(';'))) 
    

    ....