欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot使用EasyExcel导出excel

程序员文章站 2022-03-15 10:15:21
...

和Excel映射的实体类

@ExcelIgnoreUnannotated
public class PolicyItemPageVO extends BaseBean {

    /**
     * 申报项目名称
     */

    @ExcelProperty(value = "申报项目名称", index = 0)
    private String declareProjectType;

    /**
     * 开始时间
     */
    @ExcelProperty(value = "开始时间", index = 1)
    private String startTime;


    /**
     * 截止时间
     */
    @ExcelProperty(value = "截止时间", index = 2)
    private String endTime;


    /**
     * 申报状态
     */
    private String declareStatus;
    @ExcelProperty(value = "申报状态", index = 3)
    private String declareStatusName;

    /**
     * 地区
     */
    @ExcelProperty(value = "地区", index = 4)
    private String areaName;


    /**
     * 匹配关键字
     */
    @ExcelProperty(value = "匹配关键字", index = 5)
    private String keyWord;


    /**
     * 匹配关键字
     */
    @ExcelProperty(value = "自动匹配结果", index = 6)
    private String matchResult;

    /**
     * PC端链接
     *
     * @return
     */
    @ExcelProperty(value = "PC端链接", index = 7)
    private String pcUrl;

    /**
     * 手机端申报链接
     *
     * @return
     */
    @ExcelProperty(value = "手机端申报链接", index = 8)
    private String mobUrl;

    /**
     * 主管部门
     */
    @ExcelProperty(value = "部门", index = 9)
    private String competentDepartment;
}

导出方法

try {
    List<PolicyItemPageVO> list = policyItemInfoService.findBySearchParam(param);
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("名称" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")), "utf-8");
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), PolicyItemPageVO.class)
            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())//自动列宽(不太精确)
            .sheet("sheet1").doWrite(list);
} catch (Exception e) {
    e.printStackTrace();
    throw new WebapiException("导出失败");
}
相关标签: JavaEE