使用easyexcal导出excal


需要的依赖


     com.alibaba
     easyexcel
     2.1.6
 

 
     org.apache.poi
     poi-ooxml
     3.17
 

 
     org.apache.poi
     poi-ooxml-schemas
     3.17
 

 
     org.apche.poi
     poi
     3.17
 

代码,根据使用的类型和list导出

public ResponseEntity promotionMonitorListDownload(PromotionMonitorQueryDTO queryDTO) {
UserDTO currentUser = UserDTO.getCurrentUser();
try {
List list = promotionMonitorVOService.promotionMonitorListExcalDownload(currentUser, queryDTO);
Integer todayInt = ZonedDateUtil.todayInt(ZonedDateUtil.TIMEZONE_ASIN_SHANGHAI);
String fileName = new StringBuilder().append("deals管理_").append(todayInt).toString();
return buildExportResponse(fileName,PromotionMonitorExcalInfo.class,list);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultBean.fail("系统错误"));
}

}


//excal导出 private ResponseEntity buildExportResponse(String fileName, Class clazz, List list) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); EasyExcel.write(outputStream,clazz).excelType(ExcelTypeEnum.XLSX).sheet(fileName).doWrite(list); String contentDisposition = "attachment;filename=".concat(URLEncoder.encode(fileName, "UTF-8")).concat(".xlsx"); Resource resource = genResource(outputStream); return ResponseEntity.status(HttpStatus.OK) .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition) .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength())) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } private Resource genResource(ByteArrayOutputStream outputStream) { Resource resource = null; try { resource = new ByteArrayResource(outputStream.toByteArray()); } finally { IOUtils.closeQuietly(outputStream); return resource; } }



使用的实体类上打好注解作为表头部分,例如

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PromotionMonitorExcalInfo {

    @ExcelProperty("Deal ID")
    @ApiModelProperty(value = "Deal ID")
    private String dealID;private String dealPrice;
    @ExcelProperty("时间")
    @ApiModelProperty(value = "时间")
    private String ntime;
    @ExcelProperty("Sales")
    @ApiModelProperty(value = "Sales")
    private String trustee;
    @ExcelProperty("创建时间")
    @ApiModelProperty(value = "创建时间")
    private String createTimeFormat;

}

相关