如果字段中有逗号,但整体用引号括起来,那么我不应该将其视为列分隔符。如何才能做到这一点?
示例aaaa, "bb,bb", cccc
,我得到 aaaa | bb | bb |ccc
我怎样才能收到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();
}