Skip to content

CSV export

清沐 edited this page Feb 21, 2020 · 1 revision

Export in general mode

try(Csv csv = CsvBuilder.of(People.class).build(getDataList())){
    AttachmentExportUtil.export(csv.getFilePath(), "test.csv", response);
    // csv.write(Paths.get("/User/append.csv"));
}

Map export

Map type is Map<String,Object>

try(Csv csv = CsvBuilder.of(Map.class).build(getMapList())){
    AttachmentExportUtil.export(csv.getFilePath(), "test.csv", response);
    // csv.write(Paths.get("/User/append.csv"));
}

Append method export

try(CsvBuilder<CsvPeople> csvBuilder = CsvBuilder.of(People.class)){
    for (int i = 0; i < 10; i++) {
        csvBuilder.append(data(1000));
    }
    Csv csv = csvBuilder.build();
    AttachmentExportUtil.export(csv.getFilePath(), "test.csv", response);
    // csv.write(Paths.get("/User/append.csv"));
}

Secondary document addition

try(CsvBuilder<CsvPeople> csvBuilder = CsvBuilder.of(People.class).noTitles()){
    for (int i = 0; i < 10; i++) {
        csvBuilder.append(data(1000));
    }
    Csv csv = csvBuilder.build();
    // This method will continue to append data in the original append.csv file instead of overwriting
    csv.write(Paths.get("/User/append.csv"),true);
}

Bean Definition

public class People {

    @ExcelColumn(order = 0, title = "姓名")
    private String name;

    @ExcelColumn(order = 1, title = "年龄")
    private Integer age;

    @ExcelColumn(order = 2, title = "性别")
    private String gender;
}

Compared with Excel export annotation, support attributes are reduced, width and style attributes are not supported. Please move to annotation

Clone this wiki locally