如何在使用 CSVFormat 生成 CSV 时在字符串 header 中添加双引号和三重引号

How to add both double quotes and triple Quotes in String header while generating CSV using CSVFormat

我正在尝试使用 CSVFormat 生成 CSV 文件,我的要求是在某些 header 中添加双引号,在某些 header 中添加三引号。我可以在所需的 header 中添加双引号,但我无法使用 CSVFormat 在提到的 header 中添加三引号。

下面是例子

"Employee Name","Job Title","Store:","""Stage of Art""",Action Required ,,External Key..etc 

在上面的示例中,员工姓名、职位 是双引号,艺术舞台 是三引号。 我可以在 header 字段中添加双引号,下面是我编写的代码

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))