可以通过字段数组的集合或者通过实体对象集合生成csv文件。

1、数组集合生成

  1. @PostMapping("/writeCsvFile")
  2. public void writeCsvFile(String filePath) throws IOException {
  3. List<String[]> list = new ArrayList<>();
  4. list.add(new String[]{"id","short_name","name","remark","parent_id","type_name","type_id"});
  5. list.add(new String[]{"1","","大型汽车号牌","1.00","","号牌种类","1"});
  6. list.add(new String[]{"2","","小型汽车号牌","2.00","","号牌种类","1"});
  7. list.add(new String[]{"3","","使馆汽车号牌","3.50","","号牌种类","1"});
  8. @Cleanup ICSVWriter icsvWriter = new CSVWriterBuilder(new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK))
  9. .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
  10. .withQuoteChar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
  11. .build();
  12. icsvWriter.writeAll(list);
  13. }

生成csv文件

  1. id,short_name,name,remark,parent_id,type_name,type_id
  2. 1,,大型汽车号牌,1.00,,号牌种类,1
  3. 2,,小型汽车号牌,2.00,,号牌种类,1
  4. 3,,使馆汽车号牌,3.50,,号牌种类,1

2、DTO集合生成

2.1、带有文件头,通过字段名映射生成文件头

  1. @PostMapping("/writeCsvFileByName")
  2. public void writeCsvFileByName(String filePath) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
  3. List<CarCsvDTOWriteByName> list = new ArrayList<>();
  4. list.add(new CarCsvDTOWriteByName()
  5. .setId("1")
  6. .setName("大型汽车号牌")
  7. .setRemark("1.00")
  8. .setTypeName("号牌种类")
  9. .setTypeId("1"));
  10. list.add(new CarCsvDTOWriteByName()
  11. .setId("2")
  12. .setName("小型汽车号牌")
  13. .setRemark("2.00")
  14. .setTypeName("号牌种类")
  15. .setTypeId("1"));
  16. list.add(new CarCsvDTOWriteByName()
  17. .setId("3")
  18. .setName("使馆汽车号牌")
  19. .setRemark("3.50")
  20. .setTypeName("号牌种类")
  21. .setTypeId("1"));
  22. HeaderColumnNameMappingStrategy<CarCsvDTOWriteByName> strategy = new HeaderColumnNameMappingStrategy<>();
  23. strategy.setType(CarCsvDTOWriteByName.class);
  24. /*
  25. 定义头字段的顺序
  26. 实体类注解中指定的字段名在下面字符串中的位置大小决定头字段的顺序
  27. 必须大写
  28. */
  29. String headers = "ID|SHORT_NAME|NAME|REMARK|PARENT_ID|TYPE_NAME|TYPE_ID";
  30. strategy.setColumnOrderOnWrite(Comparator.comparingInt(headers::indexOf));
  31. @Cleanup Writer writer = new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK);
  32. StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
  33. .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
  34. .withQuotechar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
  35. .withMappingStrategy(strategy) // 映射策略
  36. .build();
  37. beanToCsv.write(list);
  38. }
  1. @Data
  2. @Accessors(chain = true)
  3. public class CarCsvDTOWriteByName {
  4. @CsvBindByName(column = "id")
  5. private String id;
  6. @CsvBindByName(column = "short_name")
  7. private String shortName;
  8. @CsvBindByName(column = "name")
  9. private String name;
  10. @CsvBindByName(column = "remark")
  11. private String remark;
  12. @CsvBindByName(column = "parent_id")
  13. private String parentId;
  14. @CsvBindByName(column = "type_name")
  15. private String typeName;
  16. @CsvBindByName(column = "type_id")
  17. private String typeId;
  18. }
  1. ID,SHORT_NAME,NAME,REMARK,PARENT_ID,TYPE_NAME,TYPE_ID
  2. 1,,大型汽车号牌,1.00,,号牌种类,1
  3. 2,,小型汽车号牌,2.00,,号牌种类,1
  4. 3,,使馆汽车号牌,3.50,,号牌种类,1

2.2、不带文件头,指定字段位置

  1. @PostMapping("/writeCsvFileByPosition")
  2. public void writeCsvFileByPosition(String filePath) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
  3. List<CarCsvDTOWriteByPosition> list = new ArrayList<>();
  4. list.add(new CarCsvDTOWriteByPosition()
  5. .setId("1")
  6. .setName("大型汽车号牌")
  7. .setRemark("1.00")
  8. .setTypeName("号牌种类")
  9. .setTypeId("1"));
  10. list.add(new CarCsvDTOWriteByPosition()
  11. .setId("2")
  12. .setName("小型汽车号牌")
  13. .setRemark("2.00")
  14. .setTypeName("号牌种类")
  15. .setTypeId("1"));
  16. list.add(new CarCsvDTOWriteByPosition()
  17. .setId("3")
  18. .setName("使馆汽车号牌")
  19. .setRemark("3.50")
  20. .setTypeName("号牌种类")
  21. .setTypeId("1"));
  22. ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
  23. strategy.setType(CarCsvDTOWriteByPosition.class);
  24. @Cleanup Writer writer = new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK);
  25. StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
  26. .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
  27. .withQuotechar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
  28. .withMappingStrategy(strategy) // 映射策略
  29. .build();
  30. beanToCsv.write(list);
  31. }
  1. @Data
  2. @Accessors(chain = true)
  3. public class CarCsvDTOWriteByPosition {
  4. @CsvBindByPosition(position = 0)
  5. private String id;
  6. @CsvBindByPosition(position = 1)
  7. private String shortName;
  8. @CsvBindByPosition(position = 2)
  9. private String name;
  10. @CsvBindByPosition(position = 3)
  11. private String remark;
  12. @CsvBindByPosition(position = 4)
  13. private String parentId;
  14. @CsvBindByPosition(position = 5)
  15. private String typeName;
  16. @CsvBindByPosition(position = 6)
  17. private String typeId;
  18. }
  1. 1,,大型汽车号牌,1.00,,号牌种类,1
  2. 2,,小型汽车号牌,2.00,,号牌种类,1
  3. 3,,使馆汽车号牌,3.50,,号牌种类,1

参考链接