Spring Boot整合EasyExcel,动态导出表头和数据
前端页面设置了列表表头 的动态查询,用户可以自己设置那些需要关注的字段,为此,后端需要保持导出的表头与前端一致。
本文介绍如何使用spring boot+easyExcel,动态导出数据。
步骤1.设置实体类
@DatapublicclassRepairWorkOrderextendsBaseEntity{//自增主键IDprivateIntegerid;//工单号码@TableField(condition=SqlCondition.LIKE)privateStringorderNo;//设备SN@TableField(condition=SqlCondition.LIKE)privateStringdeviceSn;//设备型号@TableField(condition=SqlCondition.LIKE)privateStringdeviceModel;//设备类型privateStringdeviceType;//工单类型(0-售后工单、1-自制工单)privateIntegertype;}步骤2.设置导出实体
@SuppressWarnings("serial")@Data@ColumnWidth(20)publicclassRepairWorkOrderExportDTO{@ExcelProperty(value="工单号")privateStringorderNo;//工单状态:0-待定性定责 1-待分配维修工程师 2-待维修测试 3-待交付 4-已完成@ExcelProperty(value="工单状态")privateStringstatusString;privateIntegerstatus;@ExcelProperty(value="设备SN")privateStringdeviceSn;//设备型号@ExcelProperty(value="设备型号")privateStringdeviceModel;//工单类型(0-售后工单、1-自制产品维修)@ExcelProperty(value="工单类型")privateStringtypeString;privateIntegertype;}步骤3.加依赖(EasyExcel Spring Boot Starter)
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel-spring-boot-starter</artifactId><version>3.3.2</version></dependency>步骤4.动态导出工具方法
数据列表:dataList
导出表头列表:includeColumnFieldNames,这里使用LinkedHashSet,保证导出表头的顺序和传入的参数顺序一致。
响应:response
publicvoidexportDynamicExcel(List<RepairWorkOrderExportDTO>dataList,LinkedHashSet<String>includeColumnFieldNames,HttpServletResponseresponse)throwsIOException{if(ObjectUtil.isNull(dataList)){thrownewValidateException("导出参数不能为空");}if(ObjectUtil.isEmpty(includeColumnFieldNames)){thrownewValidateException("导出字段列表不能为空");}response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");StringfileName="维修工单列表_"+System.currentTimeMillis()+".xlsx";response.setHeader("Content-Disposition","attachment; filename=\""+java.net.URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+","%20")+"\"");try(OutputStreamout=response.getOutputStream()){ExcelWriterBuilderwriterBuilder=EasyExcel.write(out,RepairWorkOrderExportDTO.class).includeColumnFiledNames(includeColumnFieldNames).registerWriteHandler(newLongestMatchColumnWidthStyleStrategy()).registerConverter(LocalDateStringConverter.INSTANCE)//Date日期转化工具,如无date类型可以去掉.registerConverter(LocalDateTimeStringConverter.INSTANCE)//DateTime日期转化工具,如无dateTime类型可以去掉;writerBuilder.sheet("维修工单列表").doWrite(dataList);}catch(Exceptione){log.error("导出异常",e);thrownewValidateException("导出异常");}}步骤5.状态等字段翻译成文字
这里我就使用最简单的方法了,遍历然后翻译。
privatevoidtranslateStatus(List<RepairWorkOrderExportDTO>list){if(list==null||list.isEmpty()){return;}for(RepairWorkOrderExportDTOdto:list){if(dto==null){continue;}dto.setTypeString(dto.getType()==0?Constant.type0:Constant.type1);dto.setStatusString(STATUS_MAP.getOrDefault(dto.getStatus(),"未知状态"));IntegerwarrantyStatus=dto.getWarrantyStatus();StringdeviceType=dto.getDeviceType();if(deviceType!=null){if(ServiceOrderEnum.UAV.getDescEN().equals(dto.getDeviceType())){dto.setDeviceType(ServiceOrderEnum.UAV.getDesc());}if(ServiceOrderEnum.LOAD.getDescEN().equals(dto.getDeviceType())){dto.setDeviceType(ServiceOrderEnum.LOAD.getDesc());}if(ServiceOrderEnum.NEST.getDescEN().equals(dto.getDeviceType())){dto.setDeviceType(ServiceOrderEnum.NEST.getDesc());}}}}