Springboot中CSVUtils的案例

public class CsvUtils {
    /**
     * @Description: 返回一个唯一的文件名
     * @Author: wuyc
     * @Date: 2022/3/16 11:18
     * @return java.lang.String
     **/
    public static synchronized String getUniqueKey() {
        Random random = new Random();
        Integer nu = random.nextInt(900) + 100;
        return String.valueOf(System.currentTimeMillis()).concat(String.valueOf(nu));
    }

    /**
     * @Description: 向指定路径生成csv文件,通过映射器方式
     * @Author: wuyc
     * @Date: 2022/3/16 11:11
     * @param [dataList, clazz, path, header] 数据列表, 实体类, 文件路径, 字段排序字符串(header1|header2|header3)
     * @return int
     **/
    public static <T> int writeCsv(List<T> dataList, Class<T> clazz, File file, String header) {
        try {
            Writer writer = new FileWriter(file);
            // 手动添加BOM标识
            writer.write(new String(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}));
            // 设置显示顺序
            HeaderColumnNameMappingStrategy<T> strategy = new HeaderColumnNameMappingStrategy<>();
            // 设置目标转换类型
            strategy.setType(clazz);
            // 设置表头顺序
            strategy.setColumnOrderOnWrite(Comparator.comparingInt(header::indexOf));
            // 写表头, 后期考虑是否可以删掉
            CSVWriter csvWriter = new CSVWriter(writer);

            StatefulBeanToCsv<T> beanToCsv = new StatefulBeanToCsvBuilder<T>(writer)
                    .withMappingStrategy(strategy)
                    .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                    .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
                    .withEscapechar('\\')
                    .build();
            beanToCsv.write(dataList);
            csvWriter.close();
            writer.close();
            return 1;
        } catch (Exception e){
            return 0;
        }
    }

    /**
     * @Description: 通过索引方式向指定路径生成CSV文件
     * @Author: wuyc
     * @Date: 2022/3/16 11:11
     * @param [dataList, clazz, path, header] 数据列表, 实体类, 文件路径, 字段排序字符串({"header1", "header2", "header3"})
     * @return int
     **/
    public static <T> int writeCsvByPosition(List<T> dataList, Class<T> clazz, File file, String[] header){
        try{
            Writer writer=new FileWriter(file);

            //手动添加BOM标识
            writer.write(new String(new byte[]{(byte)0xEF,(byte)0xBB,(byte)OxBF}));
            ColumnPositionMappingStrategy<T> strategy=new ColumnPositionMappingStrategy<>();
            strategy.setType(clazz);
            CSVWRiter csvWriter=new CsvWriter(writer);
            csvWriter.writeNext(header);

            StatefulBeanToCsv<T> beanToCsv=new StatefulBeantoCsvBuilder<T>(writer)
                    .withMappingStrategy(strategy)
                    .withQuotechar(CsvWriter.NO_QUOTE_CHARACTER)
                    .withSeparator(CsvWriter.DEFAULT_SEPARATOR)
                    .withEscapechar('\\').build();
            beanToCsv.write(dataList);
            csvWriter.close();
            writer.close();
            return 1 ;
        } catch(Exception e){
            return O;
        }
    }

    /**
     * @Description: 取文件路径
     * @Author: wuyc
     * @Date: 2022/3/16 11:11
     * @return String
     */
    public static String getDesktopPath(){
        FileSystemView view= FileSystemView.getFileSystemView();
        File file=view.getHomedirectory();
        return file.getPath();
    }
}

扫描二维码,在手机上阅读!

发表评论

电子邮件地址不会被公开。 必填项已用*标注