CSV打印机在行首和行尾添加双引号



我正在使用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) {
...
}

最新更新