EasyPoi——导出导入表格数据工具(解决groupName无法获取数据问题)


文档

官方文档

概念

Easypoi主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,PDF模板,通过简单的注解和模板语言,完成功能。

环境搭建(maven)

<!--导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.1.0</version>
</dependency>
<!--耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.1.0</version>
</dependency>
<!--基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.1.0</version>
</dependency>

工具类


/**
     * 导出Excel
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> entity, String fileName, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        //冻结表头
        exportParams.setCreateHeadRows(true);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entity, list);
        if (workbook == null) {
            throw new RuntimeException("Excel表导出失败");
        }
        OutputStream outputStream = null;
        BufferedOutputStream buffOutputStream = null;
        try {
            // 指定下载的文件名--设置响应头
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");
            //response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setCharacterEncoding("UTF-8");
            // 导出Excel
            outputStream = response.getOutputStream();
            buffOutputStream = new BufferedOutputStream(outputStream);
            workbook.write(buffOutputStream);
            buffOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (buffOutputStream != null) {
                    buffOutputStream.close();
                }
                if (workbook != null) {
                    workbook.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 导入
     * @param file 需要导入的文件
     * @param titleRows 标题占几行
     * @param headerRows 头部占几行
     * @param pojoClass 转化为对应的实体类
     * @return 返回解析后的实体类对象集合
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        if (file == null){
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

主要使用注解

作用于实体类
@Excel 作用到filed上面,是对Excel一列的一个描述
@ExcelCollection 表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示

@Excel 主要用的注解

  • name —— 导出时表格内的描述
  • orderNum ——导出时所在的列
  • groupName ——导出时合并单元格的父级描述
  • fixedIndex——导入时强制获取某一列的数据(如果没有,导入数据时获取合并单元格的数据会有问题,我找了半天)
    想看具体的注解就看一下官方文档。

实战

实体类(部分)

在这里插入图片描述

导出

Controller层

    @ApiOperation(value = "下载模板")
    @GetMapping("/exportLz")
    public void exportLzExcel(HttpServletResponse response, String fileName, String title){
        List<LzExcelVo> lzExcelVos = new ArrayList<>();//这里可以改为导出数据
        String sheetName= "sheet1";
        String tl = title+"表";
        ExcelUtil.exportExcel(lzExcelVos,tl,sheetName, LzExcelVo.class,fileName,response);
    }

导出结果

在这里插入图片描述

导入

service层

    /**
     * Excel表导入数据
     * @param file
     * @param qydm
     * @return
     */
    @Override
    public List<String> importLzExcel(MultipartFile file, String qydm) {
        //解析excel表数据
        List<LzExcelVo> lzExcelVos = ExcelUtil.importExcel(file,1,2,LzExcelVo.class);
        //下面就是数据处理的逻辑了
        return null;
    }

controller层

    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public ReturnWrapper<List<String>> importLz(@RequestParam MultipartFile file, String qydm){
        return ReturnWrapMapper.ok(iLzService.importLzExcel(file,qydm));
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值