一般方式导出

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

Map导出

Map类型为Map<String,Object>

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

追加方式导出

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

二次文件追加

  1. try(CsvBuilder<CsvPeople> csvBuilder = CsvBuilder.of(People.class).noTitles()){
  2. for (int i = 0; i < 10; i++) {
  3. csvBuilder.append(data(1000));
  4. }
  5. Csv csv = csvBuilder.build();
  6. // 该种方式会在原append.csv文件继续追加数据,而不是覆盖
  7. csv.write(Paths.get("/User/append.csv"),true);
  8. }

Bean定义

  1. public class People {
  2. @ExcelColumn(order = 0, title = "姓名")
  3. private String name;
  4. @ExcelColumn(order = 1, title = "年龄")
  5. private Integer age;
  6. @ExcelColumn(order = 2, title = "性别")
  7. private String gender;
  8. }

与excel导出注解相比,支持属性减少,width、style属性将不被支持,注解请移步:注解