0

我正在使用 univocity CSV 解析器将我的内容导出到 CSV 文件。这是我预期的 CSV 文件输出,

#Comment Section-1
#Comment Section-2
Header-1,Header2,Header3
Data-1,data-2,data-3

但是在将内容导出到 CSV 文件时,我得到了以下格式,

Header-1,Header2,Header3
#Comment Section-1
#Comment Section-2
Data-1,data-2,data-3

如何将评论部分移到 univocity CSV 解析器库的顶部?

4

1 回答 1

0

这应该有效:

    //let's write to a String to make this easy to test
    StringWriter out = new StringWriter();

    CsvWriterSettings settings = new CsvWriterSettings();
    //set headers
    settings.setHeaders("a", "b", "c");
    //automatically write headers when the first data row is written
    settings.setHeaderWritingEnabled(true);

    CsvWriter writer = new CsvWriter(out, settings);
    //write comment
    writer.commentRow("lalala");

    //write data rows
    writer.writeRow("1", "3", "6");
    writer.writeRow("3", "1", "1");

    writer.close();

    //print output
    System.out.println(out);

产生的输出是:

#lalala
a,b,c
1,3,6
3,1,1

希望这可以帮助。

于 2017-02-07T08:34:58.180 回答