【开源项目】使用EasyPoi动态列导出

该文章介绍了一个使用Java实现动态导出Excel的方案。通过创建ExportUtil工具类和导出接口,可以根据不同的条件动态确定导出列。在工具类中,利用ExcelExportUtil生成工作簿,并通过HttpServletResponse响应给客户端下载。文章还展示了如何定义Excel类型的枚举以及构建数据和表头的映射关系。
摘要由CSDN通过智能技术生成

业务场景

一些导出功能的导出列需要根据不同的条件动态导出
在这里插入图片描述

实现方案

增加导出工具类

public class ExportUtil {

    public static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel(fileName, response, workbook);
    }

    public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Excel 类型枚举
     */
    enum ExcelTypeEnum {
        XLS("xls"), XLSX("xlsx");
        private String value;

        ExcelTypeEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}

增加导出接口

    @GetMapping("dynamicExport")
    public void dynamicExport(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        map.put("one", "1");
        map.put("two", "2");
        map.put("three", "3");
        map.put("four", "4");
        Map<String, Object> map1 = new HashMap<>();
        map1.put("one", "11");
        map1.put("two", "21");
        map1.put("three", "31");
        map1.put("four", "41");
        //maps 存储数据
        List<Map<String, Object>> maps = new ArrayList<>();
        maps.add(map);
        maps.add(map1);
        //entityList 存表头
        List<ExcelExportEntity> entityList = new ArrayList<>();
        entityList = getEntityList();
        Workbook workbook = ExcelExportUtil.exportExcel(
                new ExportParams("test", "testName", ExcelType.HSSF)
                , entityList
                , maps);
        ExportUtil.downLoadExcel("学生列表", response, workbook);
    }

    private List<ExcelExportEntity> getEntityList() {
        List<ExcelExportEntity> entityList = new ArrayList<>();
        entityList.add(new ExcelExportEntity("姓名", "one", 20));
        entityList.add(new ExcelExportEntity("年龄", "two", 20));
        entityList.add(new ExcelExportEntity("地址", "three", 20));
        entityList.add(new ExcelExportEntity("日期", "four", 20));
        return entityList;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值