【问题标题】:Text qualifier - invalid char between tokn encapsulated and delimiter文本限定符 - 封装的令牌和分隔符之间的无效字符
【发布时间】:2021-03-22 09:54:42
【问题描述】:

如果字段中有逗号,但整体用引号括起来,那么我不应该将其视为列分隔符。如何做到这一点?

例如 aaaa, "bb,bb", cccc 我得到 aaaa | bb | bb |ccc

我怎样才能收到aaaa | "bb,bb" |抄送?

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();
    }

【问题讨论】:

  • 检查“,当检测到它时,您需要将所有内容视为严格字符串,直到检测到另一个“。您可以为此使用状态。
  • CSVFormat.EXCEL.withQuote(';') 这不是正确的引号字符。最好使用已经有正确设置的CSVFormat.EXCEL
  • @Aaron 所以我必须将其设置为包含报价?像这样 ? CSVParser csvParser = new CSVParser(reader, CSVFormat.EXCEL.withQuote('"').withDelimiter(';'))
  • 我只使用CSVFormat.EXCEL,查看我之前评论中的链接,您会看到它的默认配置适合您的数据(withDelimiter(',')withQuote('"')
  • @Aaron 以前,我只使用 CSVFormat.EXCEL 配置,但它对我不起作用。它可能取决于编码吗?或者这个方法可能是错误的 csvToXlsx?

标签: java apache-commons-csv


【解决方案1】:
 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(';'))) 

....

【讨论】:

    【解决方案2】:

    使用最新版本的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2012-06-23
      相关资源
      最近更新 更多