java 书面 CSV 文件每行仅写入一个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17749245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:52:51  来源:igfitidea点击:

Written CSV file writes only one cell per row

javacsv

提问by EoiFirst

I've written a Java class to generate CSV file :

我编写了一个 Java 类来生成 CSV 文件:

public class CSVGenerator {

    private static String REFERRAL_HEADER = "Position,Mail,Invitations,Qualified invitations";
    private static String TOPS_HEADER = "Position,Mail,Number of conferences,Number of new contacts, Average conf duration";

    public static String generate(String source, JsonNode root) throws IOException {
        String result = "";
        CSVWriter writer = new CSVWriter(new FileWriter(source+".csv"));
        if (source.compareTo("referral") == 0)
            result = REFERRAL_HEADER;
        else if (source.compareTo("tops") == 0)
            result = TOPS_HEADER;
        writer.writeNext(result.split(","));
        Iterator<JsonNode> it = root.getElements();
        while (it.hasNext()) {
            Iterator<JsonNode> it2 = it.next().getElements();
            result = "";
            while (it2.hasNext())
            {
                result += it2.next().asText();
                if (it2.hasNext())
                    result += ",";
            }
            writer.writeNext(result.split(","));
        }
        writer.close();
        return result;
    }
}

The resulting file isn't well formated. There's only one cell by row, containing all the cell and the separating comma like this :

生成的文件格式不正确。只有一个单元格,包含所有单元格和分隔逗号,如下所示:

header1,header2,header3
1,support,test
2,alpha,gamma

What am I doing wrong ?

我究竟做错了什么 ?

EDIT:

编辑:

To be more precise on the output

为了更精确地输出

I have : "header1,header2,header3","",""

我有:"header1,header2,header3","",""

I want : "header1","header2","header3"

我想要:“header1”、“header2”、“header3”

Here's my output. Each row in one unique cell.

这是我的输出。一个唯一单元格中的每一行。

   ""Position","Mail","Invitations","Qualified invitations""
    ""1","xxxxx.com","129","23""
    ""2","xxxx.com","54","8""
    ""3","xxxx.com","197","5""
    ""4","xxxx","13","5""
    ""5","xxxx","8","4""
    ""6","xxxx.com","4","4""
    ""7","xxx.com","31","3""
    ""8","xxx.com","30","3""
    ""9","xxx.com","18","3""
    ""10","xxx.com","13","3""

回答by Sadbrute

Maybe this would be better. Cheers!

也许这样会更好。干杯!

result += \"+data.asText()+\";

回答by WattoWatto

Did you try putting quotes around the result attribute;

您是否尝试在结果属性周围加上引号;

result += "\""+data.asText()+"\"";

Bearing in mind I'm not familiar with jsondata object.

请记住,我不熟悉 jsondata 对象。

Also you should write each line by line rather than appending everything at the end.

此外,您应该逐行编写每一行,而不是在末尾附加所有内容。

And yes, have a look at the builtin CSVReader and CSVWriter.

是的,看看内置的 CSVReader 和 CSVWriter。

回答by Rob Smith

Try adding quotes around the header fields:

尝试在标题字段周围添加引号:

private static String REFERRAL_HEADER = "\"Position\",\"Mail\",\"Invitations\",\"Qualified invitations\"\n";
private static String TOPS_HEADER = "\"Position\",\"Mail\",\"Number of conferences\",\"Number of new contacts\", \"Average conf duration\"\n";

回答by CaTalyst.X

I tried a subset of your code in a groovy shell and I got correct results:

我在一个 groovy shell 中尝试了你的代码的一个子集,我得到了正确的结果:

groovy:000> string = new java.io.StringWriter()
===> 
csv = new CSVWriter(string)
===> au.com.bytecode.opencsv.CSVWriter@4d9ac0b4
groovy:000> csv.writeNext(REFERRAL_HEADER.split(","))
===> null
groovy:000> string.toString()
===> "Position","Mail","Invitations","Qualified invitations"

You might try using commons-csvinstead of that CSVWriter. That library is a little funky because it is tailored to databases.

您可以尝试使用commons-csv而不是 CSVWriter。这个库有点奇怪,因为它是为数据库量身定做的。