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

java导出excel表格 使用alibaba easyexcel

程序员文章站 2024-03-20 13:51:46
...

开发中有好多时候需要导出表格,以往通常使用poi 这些jar进行倒入导出。
最近发现 阿里巴巴的一个 easyexcel 导出非常方便 记录下
项目地址

先添加依赖


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beta5</version>
        </dependency>

controller 层

   public void  downloadPointsList(@RequestParam(defaultValue = "0",required = false) int type ,
                                    @RequestParam(defaultValue = "",required = false) String  begintime,
                                    @RequestParam(defaultValue = "",required = false) String  endtime
                                    , HttpServletResponse response) throws IOException {
        List<MemberPoints> downLoads = memberPointsService.downloadBytime(type,begintime,endtime);
        OutputStream out = response.getOutputStream();
        ExcelWriter writer =new ExcelWriter(out, ExcelTypeEnum.XLSX);
        Sheet sheet1 =new Sheet(1, 0, MemberPoints.class);
        sheet1.setSheetName("会员积分");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" +new String(( "会员积分数据.xlsx").getBytes(), "ISO8859-1"));
        writer.write(downLoads, sheet1);
        writer.finish();
        out.flush();
        out.close();
    }



这里阿里是根据 实体类的注解 去生成表头对应的

public class MemberPoints  extends BaseRowModel {

    /**
     * ID
     */
     @ExcelProperty(value = "ID",index = 0)
    private Long id;

    /**
     * 会员ID
     */
    @ExcelProperty(value = "会员ID",index = 1)
    private Long memberId;

    /**
     * 类型 1获得 2 支出
     */
     @ExcelProperty(value = "类型",index =5)
    private Integer type;


}