我正在尝试使用CSVFormat生成一个CSV文件,我的要求是在某些标头中添加双引号,在一些标头中添加三引号。我可以在所需的标题中添加双引号,但无法使用CSVFormat在上述标题中添加三引号。
以下是示例
"Employee Name","Job Title","Store:","""Stage of Art""",Action Required ,,External Key..etc
在上面的示例中,员工姓名、职务用双引号括起,Stage of Art则用三引号括起。我可以在标题字段中添加双引号,下面是我写的代码
try(CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withAllowDuplicateHeaderNames("""+EmplyeeEnum.EMP_NAME.getHeader()+""","""+EmplyeeEnum.JOB_TITLE.getHeader()+""","""+EmplyeeEnum.STAGE_OF_ART.getHeader()+""",EmplyeeEnum.ACTION_REQUIRED.getHeader(),EmplyeeEnum.EXTERNAL_KEY.getHeader()).withEscape('\').withQuoteMode(Quote.NONE)
上面的代码将生成类似于下面的输出
"Employee Name","Job Title","Store:","Stage of Art",Action Required ,,External Key..etc
有人能建议我如何解决吗
这应该会给您带来预期的结果。
new CSVPrinter(w, CSVFormat.DEFAULT
.withHeader(""Employee Name"",""Job Title"",""Store:"",""""Stage of Art"""","Action Required","","External Key").withEscape('\').withQuoteMode(QuoteMode.NONE))