SpringBoot集成easypoi导出Excel

本文主要介绍使用easypoi导出固定表头的Excel

1、pom文件添加依赖

        
            cn.afterturn
            easypoi-base
            3.2.0
        
        
            cn.afterturn
            easypoi-web
            3.2.0
        
        
            cn.afterturn
            easypoi-annotation
            3.2.0
        

2、实体映射

name:导出每一列的名字

width:设置宽带

orderNum:序号,可以从0或1开始

replace:转换替代作用,下图0可转换否,1转换成是

format:时间格式参数格式化,如果时间是字符串,不需要加

@Data
public class DriverAnalysisExcel {
    @Excel(name = "姓名",width = 15,orderNum = "0")
    private String peopleName;

    @Excel(name = "身份证号",width = 25,orderNum = "1")
    private String idCard;

    @Excel(name = "是否有车", replace = {"否_0","是_1"},width = 12, orderNum = "2")
    private String ownerVehicle;

    @Excel(name = "创建时间", width = 30, orderNum = "5",format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
}

3、代码编写

@ApiOperation(value = "导出(表格)")
    @PostMapping(value = "/exportExcel")
    public void exportExcel(@ApiParam(value = "姓名") @RequestParam(required = false,value = "people_name") String peopleName, HttpServletResponse response) {
        try {
            List list = driverAnalysisService.exportExcel(peopleName);
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("人员分析","人员分析"),DriverAnalysisExcel.class,list);
            response.setHeader("Content-Disposition", "attachment;filename*= UTF-8''"+ URLEncoder.encode("人员分析"+".xls","UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(spring,boot,excel,java)