Java生成csv文件

2020-07-27  本文已影响0人  砒霜拌辣椒

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

1、数组集合生成

@PostMapping("/writeCsvFile")
public void writeCsvFile(String filePath) throws IOException {
    List<String[]> list = new ArrayList<>();
    list.add(new String[]{"id","short_name","name","remark","parent_id","type_name","type_id"});
    list.add(new String[]{"1","","大型汽车号牌","1.00","","号牌种类","1"});
    list.add(new String[]{"2","","小型汽车号牌","2.00","","号牌种类","1"});
    list.add(new String[]{"3","","使馆汽车号牌","3.50","","号牌种类","1"});

    @Cleanup ICSVWriter icsvWriter = new CSVWriterBuilder(new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK))
            .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
            .withQuoteChar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
            .build();
    icsvWriter.writeAll(list);
}

生成csv文件

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

2、DTO集合生成

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

@PostMapping("/writeCsvFileByName")
public void writeCsvFileByName(String filePath) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
    List<CarCsvDTOWriteByName> list = new ArrayList<>();
    list.add(new CarCsvDTOWriteByName()
            .setId("1")
            .setName("大型汽车号牌")
            .setRemark("1.00")
            .setTypeName("号牌种类")
            .setTypeId("1"));
    list.add(new CarCsvDTOWriteByName()
            .setId("2")
            .setName("小型汽车号牌")
            .setRemark("2.00")
            .setTypeName("号牌种类")
            .setTypeId("1"));
    list.add(new CarCsvDTOWriteByName()
            .setId("3")
            .setName("使馆汽车号牌")
            .setRemark("3.50")
            .setTypeName("号牌种类")
            .setTypeId("1"));

    HeaderColumnNameMappingStrategy<CarCsvDTOWriteByName> strategy = new HeaderColumnNameMappingStrategy<>();
    strategy.setType(CarCsvDTOWriteByName.class);

    /*
    定义头字段的顺序
    实体类注解中指定的字段名在下面字符串中的位置大小决定头字段的顺序
    必须大写
     */
    String headers = "ID|SHORT_NAME|NAME|REMARK|PARENT_ID|TYPE_NAME|TYPE_ID";
    strategy.setColumnOrderOnWrite(Comparator.comparingInt(headers::indexOf));

    @Cleanup Writer writer = new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK);
    StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
            .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
            .withQuotechar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
            .withMappingStrategy(strategy) // 映射策略
            .build();
    beanToCsv.write(list);
}
@Data
@Accessors(chain = true)
public class CarCsvDTOWriteByName {
    @CsvBindByName(column = "id")
    private String id;

    @CsvBindByName(column = "short_name")
    private String shortName;

    @CsvBindByName(column = "name")
    private String name;

    @CsvBindByName(column = "remark")
    private String remark;

    @CsvBindByName(column = "parent_id")
    private String parentId;

    @CsvBindByName(column = "type_name")
    private String typeName;

    @CsvBindByName(column = "type_id")
    private String typeId;
}
ID,SHORT_NAME,NAME,REMARK,PARENT_ID,TYPE_NAME,TYPE_ID
1,,大型汽车号牌,1.00,,号牌种类,1
2,,小型汽车号牌,2.00,,号牌种类,1
3,,使馆汽车号牌,3.50,,号牌种类,1

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

@PostMapping("/writeCsvFileByPosition")
public void writeCsvFileByPosition(String filePath) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
    List<CarCsvDTOWriteByPosition> list = new ArrayList<>();
    list.add(new CarCsvDTOWriteByPosition()
            .setId("1")
            .setName("大型汽车号牌")
            .setRemark("1.00")
            .setTypeName("号牌种类")
            .setTypeId("1"));
    list.add(new CarCsvDTOWriteByPosition()
            .setId("2")
            .setName("小型汽车号牌")
            .setRemark("2.00")
            .setTypeName("号牌种类")
            .setTypeId("1"));
    list.add(new CarCsvDTOWriteByPosition()
            .setId("3")
            .setName("使馆汽车号牌")
            .setRemark("3.50")
            .setTypeName("号牌种类")
            .setTypeId("1"));

    ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
    strategy.setType(CarCsvDTOWriteByPosition.class);

    @Cleanup Writer writer = new FileWriterWithEncoding(filePath, CharsetUtil.CHARSET_GBK);
    StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
            .withSeparator(ICSVWriter.DEFAULT_SEPARATOR) // 分隔符
            .withQuotechar(ICSVWriter.NO_QUOTE_CHARACTER) // 不使用引号
            .withMappingStrategy(strategy) // 映射策略
            .build();
    beanToCsv.write(list);
}
@Data
@Accessors(chain = true)
public class CarCsvDTOWriteByPosition {
    @CsvBindByPosition(position = 0)
    private String id;

    @CsvBindByPosition(position = 1)
    private String shortName;

    @CsvBindByPosition(position = 2)
    private String name;

    @CsvBindByPosition(position = 3)
    private String remark;

    @CsvBindByPosition(position = 4)
    private String parentId;

    @CsvBindByPosition(position = 5)
    private String typeName;

    @CsvBindByPosition(position = 6)
    private String typeId;
}
1,,大型汽车号牌,1.00,,号牌种类,1
2,,小型汽车号牌,2.00,,号牌种类,1
3,,使馆汽车号牌,3.50,,号牌种类,1

Java解析csv文件

参考链接

代码地址

上一篇下一篇

猜你喜欢

热点阅读