繁体   English   中英

CSVPrinter 在行首和行尾添加双引号

[英]CSVPrinter adds double quotes at beginning and end of line

我正在使用 CSVPrinter 创建一个 CSV。

@Data
public class ExportRecord {

    String name;
    String email;
    String code;

    @Override
    public String toString() {
        return String.join(";",
                name,
                email,
                StringUtils.defaultString(code));   
    }

}
CSVFormat csvFormat = CSVFormat.EXCEL.builder()
                .setDelimiter(';')
                .setHeader(Arrays.stream(ExportHeader.values()).map(ExportHeader::getCsvHeader).toArray(String[]::new))
                .build();

try (CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(csvToExport), csvFormat)) {
  for (ExportRecord exportRecord : exportRecords) {
    csvPrinter.printRecord(exportRecord);
  }
} catch (IOException e) {
    ...
}

这工作正常,但它在行的开头和结尾添加了双引号。 例如

header1;header2;header3
"string1;string2;string3"

我怎样才能删除那些?

我使用 PrintWriter 尝试了一个更简单的解决方案并解决了它。

try (PrintWriter printWriter = new PrintWriter(csvToExport)) {
    String header = Arrays.stream(ExportHeader.values())
                    .map(ExportHeader::getCsvHeader)
                    .collect(joining(";"));
    printWriter.println(header);
    for (ExportRecord exportRecord : exportRecords) {
       printWriter.println(exportRecord.toString());
    }
} catch (IOException e) {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM