使用EasyExcel导出Excel

maven

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

工具类

/**
     * 公用导出下载入口
     *
     * @param list     结果集
     * @param fileName 文件名
     * @reqDTO
     */
    public static <T> void download(HttpServletResponse response, T reqVO, List<?> list, String fileName) throws Exception {

        OutputStream out = response.getOutputStream();
        try {
            ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLSX, true);
            // 设置固定在第一个sheet
            Sheet sheet1 = new Sheet(SHEET_NO, HEADLINE_MUN);
            sheet1.setSheetName(fileName);
            //or 设置自适应宽度
            sheet1.setAutoWidth(Boolean.TRUE);
            // 设置标题
            Table table = new Table(TABLE_NO);
            table.setClazz((Class<? extends BaseRowModel>) reqVO);
            // 写文件
            writer.write((List<? extends BaseRowModel>) list, sheet1, table);
            response(response, fileName, null);
            writer.finish();
            out.flush();
        } catch (Exception e) {
            logger.error("error message", e);
        } finally {
            if (null != out) {
                out.close();
            }
        }


    }

Poji 单个标题

@Data
public class UserModel extends BaseRowModel {

    @ExcelProperty(index = 0, value = "名称")
    private String name;

    @ExcelProperty(index = 1, value = "年龄")
    private String age;

    @ExcelProperty(index = 2, value = "类型")
    private String type;
 }

Poji 多行标题

@Data
public class UserModel extends BaseRowModel {

    @ExcelProperty(index = 0, value = {"部门/姓名", "部门/姓名"})
    private String name;

    @ExcelProperty(index = 1,  value = {"职位", "职位A"})
    private String position1;

    @ExcelProperty(index = 2,  value = {"职位", "职位B"})
    private String position2;
    
    @ExcelProperty(index = 3,  value = {"薪资", "薪资"})
    private String money;

 }

调用

    /**
     * 人员导出
     *
     * @return
     */
    @RequestMapping(value = "/export")
    public void export(@ApiParam(value = "查询请求对象", required = true) ReportVO vo, HttpServletResponse response) throws Exception {
        
        List<UserModel> list = reportRSV.queryList(vo);
 
        ExcelUtils.download(response, UserModel.class, list, "人员导出");

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值