如果字段中有逗号,但整个字段都用引号封闭,那么我不应该将其视为列分隔符。如何做到这一点?
示例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();
}
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(';')))
在使用最新版本的commons-csv-1.8时,以下内容对我有效:
Reader in = new StringReader("aaaa,"bb,bb",cccc");
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withDelimiter(',').withQuote('"').parse(in);
for (CSVRecord record : records) {
for (int i = 0; i < record.size(); i++) {
System.out.println("At " + i + ": " + record.get(i));
}
}
以及使用预定义的EXCEL格式:
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);