msa*_*gel 3 java csv apache-commons
当值被引用时,我需要在模式下使用 Apache 公共资源中的 CSVPrinter,但标头不是。看起来报价模式是唯一的选项,影响标题和值。这个可以独立完成吗?
CSVFormat format = CSVFormat.DEFAULT.withHeader(new String(){"a", "b"})
.withQuoteMode(QuoteMode.ALL);
CSVPrinter printer = new CSVPrinter(new FileWriter(new File("out.csv")), format);
printer.printRecord("a val", "b val");
printer.printRecord("1", "2");
printer.flush();
printer.close();
Run Code Online (Sandbox Code Playgroud)
给出:
"a", "b"
"a val", "b val"
"1", "2"
Run Code Online (Sandbox Code Playgroud)
但要求是这样的:
a,b
"a val", "b val"
"1", "2"
Run Code Online (Sandbox Code Playgroud)
您可以使用一种格式作为标题,使用另一种格式作为记录:
FileWriter writer = new FileWriter(new File("out.csv"));
CSVFormat formatHeader = CSVFormat.DEFAULT
.withHeader(new String[]{"a", "b"})
.withEscape('"').withQuoteMode(QuoteMode.NONE);
CSVFormat formatRecord = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL);
CSVPrinter headerPrinter = new CSVPrinter(writer, formatHeader);
headerPrinter.flush();
CSVPrinter recordPrinter = new CSVPrinter(writer, formatRecord);
recordPrinter.printRecord("a val", "b val");
recordPrinter.printRecord("1", "2");
recordPrinter.flush();
recordPrinter.close();
headerPrinter.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1525 次 |
| 最近记录: |