美文网首页poi
如何使用Easypoi按模板导出Excel?

如何使用Easypoi按模板导出Excel?

作者: 小土豆哥哥 | 来源:发表于2018-12-29 14:00 被阅读0次

    Easypoi为谁开发

    • 不太熟悉poi的
    • 不想写太多重复太多的
    • 只是简单的导入导出的
    • 喜欢使用模板的

    Easypoi的目标是什么

    不是替代poi,而是让一个不懂导入导出的快速使用poi完成Excel和word的各种操作,而不是看很多api才可以完成这样的工作

    独特的功能
    • 基于注解的导入导出,修改注解就可以修改Excel
    • 支持常用的样式自定义
    • 基于map可以灵活定义的表头字段
    • 支持一堆多的导出,导入
    • 支持模板的导出,一些常见的标签,自定义标签
    • 支持HTML/Excel转换,如果模板还不能满足用户的变态需求,请用这个功能
    • 支持word的导出,支持图片,Excel

    如何使用

    1. easypoi 父包--作用大家都懂得
    2. easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理
    3. easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能
    4. .easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能

    加入maven依赖,如果没有maven请直接下jar,在alimaven

        <dependency>    
            <groupId>cn.afterturn</groupId>    
            <artifactId>easypoi</artifactId>   
            <version>3.2.0</version>   
            <type>pom</type>
        </dependency>
        <dependency>   
            <groupId>cn.afterturn</groupId>   
            <artifactId>easypoi-base</artifactId> 
            <version>3.2.0</version>
        </dependency>
        <dependency>    
            <groupId>cn.afterturn</groupId>    
            <artifactId>easypoi-annotation</artifactId>    ]
            <version>3.2.0</version>
         </dependency>
        <dependency>   
            <groupId>cn.afterturn</groupId>  
            <artifactId>easypoi-web</artifactId>  
            <version>3.2.0</version>
        </dependency>
    
    java导出接口
    /**
         * 自定义导出过磅记录excel
         *
         * @param poundLogDto 查询条件
         * @return
         */
        @RequestMapping(value = "/poundLog/exportCustomExcel", method = {RequestMethod.GET})
        @ResponseBody
        public Result exportCustomExcel(PoundLogDto poundLogDto) {
            //1.获取废品的excel模板
            TemplateExportParams params = new TemplateExportParams("doc/customLog.xlsx");
            //2.获取所有过磅数据
            List<Map<String, Object>> allMap = poundLogService.findAllMapByCondition(poundLogDto, null);
    
            Map<String, Object> map = new HashMap<String, Object>(100);
            map.put("poundLogList", allMap);
            //3.执行excel导出
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    
            //4.创建文件存储路径
            String dateStr = DateUtil.dateString("yyyy_MM_dd", new Date());
            File saveFile = new File(systemProperties.getFileLocation() + "/excel/" + dateStr);
            if (!saveFile.exists()) {
                saveFile.mkdirs();
            }
            FileOutputStream fos;
            String filePath;
            try {
                //4.写入文件
                filePath = saveFile + "\\过磅记录.xlsx";
                fos = new FileOutputStream(filePath);
                workbook.write(fos);
                fos.close();
    
                JSONObject data = new JSONObject();
                data.put("filepath", filePath);
                return ResultUtil.success("数据导出成功!", data);
            } catch (FileNotFoundException e) {
                logger.error("FileNotFoundException={}", e.getMessage());
                return ResultUtil.error("数据导出失败!" + e.getMessage());
            } catch (IOException e) {
                logger.error("IOException={}", e.getMessage());
                return ResultUtil.error("数据导出失败!" + e.getMessage());
            }
        }
        
        
     /**
         * 获取所有数据封装成mapList
         *
         * @param poundLogDto 查询条件
         * @param types       进出货类型 0:进货 1:出货 null:全选
         * @return mapList
         */
        @Override
        public List<Map<String, Object>> findAllMapByCondition(PoundLogDto poundLogDto, Integer types) {
            if (Integer.valueOf(PoundLogConstant.TYPES_IN).equals(types) || Integer.valueOf(PoundLogConstant.TYPES_OUT).equals(types)) {
                poundLogDto.setTypes(types);
            }
            List<PoundLog> poundLogList = httpService.findAllLogByCondition(poundLogDto);
            if (null == poundLogList) {
                return new ArrayList<>(0);
            }
    
            List<Map<String, Object>> mapList = new ArrayList<>(poundLogList.size());
    
            for (PoundLog poundLog : poundLogList) {
                Map<String, Object> mapOne = new HashMap<>(100);
                if (StringUtils.isNotEmpty(poundLog.getShopCode())) {
                    WorkShop workShop = workShopMapper.findByShopCode(poundLog.getShopCode());
                    poundLog.setWorkShopName(workShop.getShopName() + "-" + workShop.getOrganization().getOrgName());
                    mapOne.put("workShopName", poundLog.getWorkShopName());
                }
                mapOne.put("createTime", DateUtil.parseToFormatDateString(poundLog.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                mapOne.put("goodsName", poundLog.getGoodsName());
                mapOne.put("compName", poundLog.getCompName());
                mapOne.put("orgName", poundLog.getUnitName());
                mapOne.put("plateNo", poundLog.getPlateNo());
                mapOne.put("grossWeight", poundLog.getGrossWeight());
                mapOne.put("tareWeight", poundLog.getTareWeight());
                mapOne.put("netWeight", poundLog.getNetWeight());
                mapOne.put("diffWeight", poundLog.getDiffWeight());
                mapOne.put("returnWeightTotal", poundLog.getReturnWeightTotal());
                mapOne.put("unit", "公斤");
                mapOne.put("remark", poundLog.getRemark());
                mapOne.put("poundLogNo", poundLog.getPoundLogNo());
                mapOne.put("inspNo", poundLog.getInspNoList());
                mapOne.put("month", DateUtil.getMonth(poundLog.getCreateTime()) + "月");
                mapList.add(mapOne);
            }
    
            return mapList;
        }
    
    excel模板
    Image.png
    模板指令介绍

    模板是处理复杂Excel的简单方法,复杂的Excel样式,可以用Excel直接编辑,完美的避开了代码编写样式的雷区,同时指令的支持,也提了模板的有效性 下面列举下EasyPoi支持的指令以及作用,最主要的就是各种fe的用法

    1. 空格分割
    2. 三目运算 {{test ? obj:obj2}}
    3. n: 表示 这个cell是数值类型
    4. {{n:}}le: 代表长度{{le:()}}
    5. 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
    6. fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
    7. fn: 格式化数字 {{fn:(obj;###.00)}}
    8. fe: 遍历数据,创建row!fe: 遍历数据不创建row$fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入#
    9. fe: 横向遍历v_fe: 横向遍历值
    10. !if: 删除当前列 {{!if:(test)}}
    11. 单引号表示常量值 '' 比如'1' 那么输出的就是 1
    12. &NULL& 空格
    13. ]] 换行符
    14. 多行遍历导出
    15. sum: 统计数据

    整体风格和el表达式类似,大家应该也比较熟悉 采用的写法是{{}}代表表达式,然后根据表达式里面的数据取值

    关于样式问题

    easypoi不会改变excel原有的样式,如果是遍历,easypoi会根据模板的那一行样式进行复制

    友情链接

    相关文章

      网友评论

        本文标题:如何使用Easypoi按模板导出Excel?

        本文链接:https://www.haomeiwen.com/subject/akgulqtx.html