CSV文件生成

时间:2016-07-01 04:17:41

标签: java spring csv

    public class csvFileDownload {
    @RequestMapping(value = "/downloadCSV")
    public void downloadCSV(HttpServletResponse response) throws IOException {

        Student s2 = new Student(11, "Sachin", 30);
        Student s3 = new Student(12, "Vikas", 40);
        Student s4 = new Student(10, "Harkirat", 20);


        List<Student> std = Arrays.asList(s4,s2,s3);



        FileWriter fileWriter = new FileWriter("students.csv",true);

        writeToCSV(std, fileWriter);
        writeToCSV(std, fileWriter);
        fileWriter.close();



    }


    private void writeToCSV(List<Student> std, Writer writer) throws IOException {
        // uses the Super CSV API to generate CSV data from the model data
        ICsvBeanWriter csvWriter = new CsvBeanWriter(writer,
                CsvPreference.STANDARD_PREFERENCE);

        String[] header = {"Id", "Name", "Age"};
        csvWriter.writeHeader(header);

        for (Student aStudent : std) {
            csvWriter.write(aStudent, header);
        }
        csvWriter.close();
    }
}

我正在编写此程序并获取CSV文件并附加它。但我得到相同的标题。我怎样才能删除相同的标题,如何将代码更改为简单。 输出

Id,Name,Age
10,Harkirat,20
11,Sachin,30
12,Vikas,40
Id,Name,Age
10,Harkirat,20
11,Sachin,30
12,Vikas,40

2 个答案:

答案 0 :(得分:2)

这样的事情:

 public void downloadCSV(HttpServletResponse response) throws IOException {
    FileWriter fileWriter = new FileWriter("students.csv",true);

    // uses the Super CSV API to generate CSV data from the model data
    ICsvBeanWriter csvWriter = new CsvBeanWriter(fileWriter,
            CsvPreference.STANDARD_PREFERENCE);

    String[] header = {"Id", "Name", "Age"};
    csvWriter.writeHeader(header);

    List<Student> sList = getStudents();
    writeToCSV(sList, csvWriter);

    icbw.close();
    fileWriter.close();
}

private List<Student> getStudents()
{
    Student s2 = new Student(11, "Sachin", 30);
    Student s3 = new Student(12, "Vikas", 40);
    Student s4 = new Student(10, "Harkirat", 20);

    return Arrays.asList(s4,s2,s3);
}


private void writeToCSV(List<Student> std, ICsvBeanWriter icbw) throws IOException {
    for (Student aStudent : std) {
        icbw.write(aStudent, header);
    }
}

答案 1 :(得分:1)

您可以添加一个标志来控制是否写入标题。

public class csvFileDownload {
    @RequestMapping(value = "/downloadCSV")
    public void downloadCSV(HttpServletResponse response) throws IOException {

        Student s2 = new Student(11, "Sachin", 30);
        Student s3 = new Student(12, "Vikas", 40);
        Student s4 = new Student(10, "Harkirat", 20);

        List<Student> std = Arrays.asList(s4, s2, s3);

        FileWriter fileWriter = new FileWriter("students.csv", true);

        writeToCSV(std, fileWriter, true);
        writeToCSV(std, fileWriter, false);
        fileWriter.close();

    }

    private void writeToCSV(List<Student> std, Writer writer, boolean writeHeader) throws IOException {
        // uses the Super CSV API to generate CSV data from the model data
        ICsvBeanWriter csvWriter = new CsvBeanWriter(writer, CsvPreference.STANDARD_PREFERENCE);

        String[] header = { "Id", "Name", "Age" };
        if(writeHeader) csvWriter.writeHeader(header);

        for (Student aStudent : std) {
            csvWriter.write(aStudent, header);
        }
        csvWriter.close();
    }
}