Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java创建CSV文件_Java_Excel_Csv - Fatal编程技术网

用Java创建CSV文件

用Java创建CSV文件,java,excel,csv,Java,Excel,Csv,我需要创建一个特定格式的CSV文件。这是格式 “ROWHEAD”,2016/09/13 03:24:42 -0700,”A”,”BCDE”,002, “SECHEAD”,2016/09/12 00:00:00 -0700,2016/09/12 23:59:59 -0700,”BCDE” “COLHEAD”,”Col A”,”Col B”,”Col C”,”Col D”,”Col E”,”Col F” “SECBODY”,”val A”,”val B”,”val C”,”val D”,”val

我需要创建一个特定格式的CSV文件。这是格式

“ROWHEAD”,2016/09/13 03:24:42 -0700,”A”,”BCDE”,002,
“SECHEAD”,2016/09/12 00:00:00 -0700,2016/09/12 23:59:59 -0700,”BCDE”

“COLHEAD”,”Col A”,”Col B”,”Col C”,”Col D”,”Col E”,”Col F”
“SECBODY”,”val A”,”val B”,”val C”,”val D”,”val E”,”val F”
“SECBODY”,”val A”,”val B”,”val C”,”val D”,”val E”,”val F”
“SECBODY”,”val A”,”val B”,”val C”,”val D”,”val E”,”val F”
“SECBODY”,”val A”,”val B”,”val C”,”val D”,”val E”,”val F”

“SECFOOT”,”XXX”,0,0,0,0,”YY”,0,”ZZ”,0,189
“SECCOUNT”,1
"ROWFOOT”,”XXX”,0,0,0,0,”YY”,0,”ZZ”,0,189
我尝试过使用普通的文件编写器方式,但这并不能帮助我实现这一点。同样,我也尝试了OpenCSVAPI,这甚至没有多大帮助

如何创建包含此类页眉和页脚值的CSV文件?

开始处理此问题。它有一个
OutputValueSwitch
,它将匹配每行特定列中的一个值,以确定要使用的
RowProcessor

例如,如果您的输入行是从java Bean生成的(可能是这样),而其他一些行是对象数组的普通列表:

    OutputValueSwitch writerSwitch = new OutputValueSwitch(0); //row identifiers go at column 0

    // If the value is "ROWHEAD", we want to use an BeanWriterProcessor. You can provide field names to be associated with the fields in the class.
    writerSwitch.addSwitchForValue("ROWHEAD", new BeanWriterProcessor(RowHead.class));

    writerSwitch.addSwitchForValue("SECHEAD", new BeanWriterProcessor(SecHead.class));

    // If the value is "SECBODY", a ObjectRowWriterProcessor will be used. Let's assume you are writing object arrays here
    writerSwitch.addSwitchForValue("SECBODY", new ObjectRowWriterProcessor()); 
    //...and so on.

    //Configure the CSV writer here
    CsvWriterSettings settings = new CsvWriterSettings();
    // the writer should use the switch defined above
    settings.setRowWriterProcessor(writerSwitch);

    settings.getFormat().setLineSeparator("\n");
    settings.setHeaderWritingEnabled(false);
    //etc

    //Create the CSV writer
    CsvWriter writer = new CsvWriter(new File("/path/to/your.csv"), "UTF-8", settings);

    writer.processRecord(new RowHead()); //writing bean
    writer.processRecord(new SecHead()); //writing the other bean
    writer.processRecord(new Object[]{"SECBODY", "Value 1", "Value 2", "etc"}); //writing an array

    writer.close();
也可以将贴图用作输入行。有关完整的工作示例,请参阅和

你可以用这个图书馆做任何事,我希望它能帮助你

披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。

开始处理这个问题。它有一个
OutputValueSwitch
,它将匹配每行特定列中的一个值,以确定要使用的
RowProcessor

例如,如果您的输入行是从java Bean生成的(可能是这样),而其他一些行是对象数组的普通列表:

    OutputValueSwitch writerSwitch = new OutputValueSwitch(0); //row identifiers go at column 0

    // If the value is "ROWHEAD", we want to use an BeanWriterProcessor. You can provide field names to be associated with the fields in the class.
    writerSwitch.addSwitchForValue("ROWHEAD", new BeanWriterProcessor(RowHead.class));

    writerSwitch.addSwitchForValue("SECHEAD", new BeanWriterProcessor(SecHead.class));

    // If the value is "SECBODY", a ObjectRowWriterProcessor will be used. Let's assume you are writing object arrays here
    writerSwitch.addSwitchForValue("SECBODY", new ObjectRowWriterProcessor()); 
    //...and so on.

    //Configure the CSV writer here
    CsvWriterSettings settings = new CsvWriterSettings();
    // the writer should use the switch defined above
    settings.setRowWriterProcessor(writerSwitch);

    settings.getFormat().setLineSeparator("\n");
    settings.setHeaderWritingEnabled(false);
    //etc

    //Create the CSV writer
    CsvWriter writer = new CsvWriter(new File("/path/to/your.csv"), "UTF-8", settings);

    writer.processRecord(new RowHead()); //writing bean
    writer.processRecord(new SecHead()); //writing the other bean
    writer.processRecord(new Object[]{"SECBODY", "Value 1", "Value 2", "etc"}); //writing an array

    writer.close();
也可以将贴图用作输入行。有关完整的工作示例,请参阅和

你可以用这个图书馆做任何事,我希望它能帮助你


披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。

您在使用Java时遇到了哪些具体问题?您的代码是否有特定的问题?请使用csvwriter类来执行此操作。(opencsv)@Tim——我可以像普通的文件编写器一样编写CSV文件,如果记录变大,就很难处理它。使用OpenCSV,但它将所有内容都写为字符串,但我也有日期格式。使用Java时您遇到了哪些具体问题?您的代码是否有特定的问题?请使用csvwriter类来执行此操作。(opencsv)@Tim——我可以像普通的文件编写器一样编写CSV文件,如果记录变大,就很难处理它。使用OpenCSV,但它将所有内容都写为字符串,但我也有日期格式。谢谢@Jeronimo。我要试试这个。希望这能满足我的需要。谢谢@Jeronimo。我要试试这个。希望这能满足我的需要。