springboot实现excel下载

工具类:

public class ExcelUtil {
    /**
     * @ClassName ExcelUtil
     * @Description : 用于生成指定目录的Excel不用ReisterConverter
    */
    public static boolean generatorExcelWithoutConverter(String path, Class T, String sheetName, List<? extends Object> res){
        try{
            EasyExcel.write(path, T).sheet(sheetName).doWrite(res);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }
    /**
     * @ClassName ExcelUtil
     * @Description : 用于生成指定目录的Excel使用指定的ReisterConverter
    */

    public static boolean generatorExcelWithConverter(String path, Class T, String sheetName, List<? extends Object> res, Converter converter){
        try{
            EasyExcel.write(path, T).registerConverter(converter).sheet(sheetName).doWrite(res);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }
}

实体类:

@Accessors(chain = true)
public class ExcelByDay {
    @ExcelIgnore
    private static final long serialVersionUID = 1L;
    /**
     * 员工编号
     */
    @ExcelProperty(value = "员工编号",index = 0)
    private Integer employeeId;

    /**
     * 职工姓名
     */
    @ExcelProperty(value = "职工姓名",index = 1)
    private String employeeName;
    /**
     * 工时
     */
    @ColumnWidth(15)
    @ExcelProperty(value = "工时",index = 15,converter = LocalDateTimeLastConverter.class)
    private Double workHours;

@ColumnWidth(15)是excal的宽度
value就是属性名,
index是次序,
converter = LocalDateTimeLastConverter.class)这是时间的特殊处理,需要下方处理类:

LocalDateTime

public class LocalDateTimeConverter implements Converter<LocalDateTime> {

    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }


}

LocalDate

public class LocalDateConverter implements Converter<LocalDate> {

    @Override
    public Class<LocalDate> supportJavaTypeKey() {
        return LocalDate.class;
    }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
        }

    @Override
    public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration) {
        return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }

        @Override
        public CellData convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }

}

LocalTime

public class LocalTimeConverter implements Converter<LocalTime> {

    @Override
    public Class<LocalTime> supportJavaTypeKey() {
        return LocalTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        return LocalTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("HH:mm:ss"));
    }

    @Override
    public CellData convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(value.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
    }


}

最终使用:

    /**
     * 生成报表
     * @param days 时间
     * @param format  报表前缀
     * @param path 报表路径
     */
    private void ExcelDownload(int days,String format,String path,int sign){
        //前一周的时间
        LocalDate end =  LocalDate.now().minusDays(1);
        LocalDate start =end.minusDays(days);
        //文件名
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String filename = format +dateTimeFormatter.format(start) +".xlsx";
        //跟文件路径
        String filepath = System.getProperty("user.dir")+path+filename;
        //一周的时间列表
        List<LocalDate> timeList=new ArrayList<>();
        for (int i = 0; i < days; i++) {
            timeList.add(end.minusDays(i));
        }
        //查询出当前所有记录
        List<ExcelByDay> list = listExcelByList(timeList);
        System.out.println("----------------------------------"+list);
        //查询表中有没有
        //生成excel,先不用线程
        ExcelUtil.generatorExcelWithoutConverter(filepath,ExcelByDay.class,filename,list);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值