美文网首页
easypoi 导出excel 和word文件

easypoi 导出excel 和word文件

作者: 何事西风悲画扇_4b46 | 来源:发表于2019-07-02 20:08 被阅读0次

    easypoi的简单导出 数据map导出

    官方说明文档
    http://easypoi.mydoc.io/
    需要渲染的模版

    image.png

    语法

    循环开始 {{$fe:maplist t.ho
    循环中间参数 t.po
    换行]]
    循环结尾t.amount}}
    统计循环数量{{sum:(t.amount)}}
    单独参数{{totalQuantity}}

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

    maven

     <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-base</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-web</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-annotation</artifactId>
                <version>3.2.0</version>
            </dependency>
    

    过滤的应用场景


    .png
    @RequestMapping("exportExcel")
            @SuppressWarnings("unchecked")
            @RequiresPermissions("business:product:exportExcel")
            public R exportExcel(@RequestBody Map<String, Object> params,ModelMap model,HttpServletRequest request, HttpServletResponse response){
                List<ProductEntity> list=new ArrayList<ProductEntity>();//
                ExportParams param = new ExportParams("产品信息", "产品信息",ExcelType.XSSF);
                if(params.get("ids")!=null){
                    list=productService.queryListByIds(params.get("ids"));
                }else{
                    list=productService.queryListTwoThousand(params);
                }
                if(params.get("selected")!=null){
                     String[] s=params.get("selected").toString().split(",");//前端获取选中的栏目
                     List<String> lists =(List<String>) params.get("selected");
                     List<String> old = new ArrayList<String>();
                     ProductEntity  entity=new ProductEntity();
                     for (Field field : entity.getClass().getDeclaredFields()) {//去掉选中的栏目
                         if(!lists.contains(field.getName())){
                             old.add(field.getName()); 
                         }
                     }
                     s=  old.toArray(new String[1]);//list转数组
                     param.setExclusions(s);//过滤没有选中的栏目导出
                }
                Workbook workbook = ExcelExportUtil.exportExcel(param, ProductEntity.class,list);
                ByteArrayOutputStream fos;
                String url="";
                try {
                    fos = new ByteArrayOutputStream();
                    workbook.write(fos);
                    url=OSSFactory.build().upload(fos.toByteArray(), OSSConfig.getALI_TEMP()+"excel/product_excel"+System.currentTimeMillis()+".xlsx");
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(url.length()>0){
                    return R.ok().put("url", url).put("msg", "生成成功");
                }else{
                    return R.error().put("msg", "生成失败");
                }
            }
    

    这个是文件上传阿里云的 顺便过滤选中自己想导出不同栏目的,接下来看看实体类

    package com.alpha.modules.product.entity;
    
    import java.io.Serializable;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    import org.hibernate.validator.constraints.NotBlank;
    
    import cn.afterturn.easypoi.excel.annotation.Excel;
    
    /**
     * 产品表-pt_product
     * 
     * @author Temple
     * @email
     * @date 2018-08-08 17:00:09
     */
    public class ProductEntity implements Serializable {
        private static final long serialVersionUID = 1L;
    
        // ID
        @NotBlank(message = "参数值不能为空")
        private Long id;
    
        // 产品名称
        @Excel(name = "产品名称", orderNum = "2", width = 20)
        private String name;
    
        // 产品编号
        @Excel(name = "产品编号", orderNum = "2", width = 20)
        private String code;
    
        // 样品类别代码
        private String typeCode;
        private String typeName;
    
        // 产品分类代码
        private Integer categoryId;
        @Excel(name = "产品类别", orderNum = "3", width = 10)
        private String categoryName;
    
        // 产品分类备注
        private String categoryNote;
    
        // 主题代码
        private String themeCode;
        @Excel(name = "产品主题", orderNum = "4", width = 10)
        private String themeName;
    
        // 供应商代码
        @Excel(name = "工厂代码", orderNum = "6", width = 10)
        private String factoryCode;
        // 供应商名称
        @Excel(name = "工厂名称", orderNum = "5", width = 30)
        private String factoryName;
    
        // 产品报价
        @Excel(name = "工厂报价", orderNum = "7", width = 10)
        private Double price;
    
        // 报价币种
        @Excel(name = "币种", orderNum = "8", width = 10)
        private String currency;
    
        // 单位
        @Excel(name = "单位", orderNum = "9", width = 10)
        private String unit;
    
        // 开发人用户名
        @Excel(name = "开发人", orderNum = "10", width = 15)
        private String developUser;
    
        // 设计人用户名
        @Excel(name = "设计人", orderNum = "11", width = 15)
        private String designUser;
    
        // 产品材质
        @Excel(name = "产品材质", orderNum = "12", width = 15)
        private String material;
    
        // 工厂货号
        @Excel(name = "工厂货号", orderNum = "13", width = 15)
        private String factoryNo;
    
        // 产品描述
        @Excel(name = "产品描述", orderNum = "14", width = 35)
        private String descNote;
    
        // 图片加密:0不加密,1加密
        private Integer encrypt;
    
        // 上传时间
        @Excel(name = "录入时间", orderNum = "15", width = 20,exportFormat="yyyy-MM-dd")
        private Date ctime;
    
        // 图片url
        @Excel(name = "产品图片", orderNum = "1", imageType = 1, type = 2, width = 35, height = 49)
        private String picUrl;
    
        // 上传操作员
        @Excel(name = "录入人员", orderNum = "16", width = 15)
        private String cuser;
    
        // 唛头
        private String mark;
    
        // 状态:0禁用,1开启 默认:0
        private Integer state;
        // 图片样式
        private String picStyle;
    
        private Integer isSuit;
        private String suitCode;
    
        private String fingerprint;// 指纹信息
    
        private List<Map<String, Object>> suitList;
    
        // 产品类型:0主料,1辅料
        private Integer proType;
        
        private String customerCode;
        
        private String topCategory;
        
        private int serialNum;//序列号
        
        private int filialeId;
    }
    

    用注解 包括日期格式 数字替换文字等

        @Excel(name = "录入时间", orderNum = "15", width = 20,exportFormat="yyyy-MM-dd")
    //时间格式
    @Excel(name = "产品图片", orderNum = "1", imageType = 1, type = 2, width = 35, height = 49)
    //图片
    //数字替换成文字
    @Excel(name = "发票状态", orderNum = "1",replace = { "未推送_0", "已推送_1" }, width = 20)
    

    map封装导出比较类似

    image.png

    实体类导出excel 简单导出

    @RequestMapping("/exportPackingReportExcel")
        public void exportPackingReportExcel(ModelMap model, @RequestParam Map<String, Object> param,
                HttpServletRequest request, HttpServletResponse response) {
    
            ExportParams params = new ExportParams(null, "装箱单报表", ExcelType.XSSF);
            List<PackingReportCostEntity> dataList =packing.exportPackingReportExcel(param);
            model.put(NormalExcelConstants.DATA_LIST, dataList);
            model.put(BigExcelConstants.CLASS, PackingReportCostEntity.class);
            model.put(BigExcelConstants.PARAMS, params);
            model.put(MapExcelConstants.FILE_NAME, "装箱单报表");
            PoiViewUtil.render(model, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
        }
    

    js文件

    //参数等  这个是最简单的导出
    window.location.href="/"+T.moduleName+"/packingReport/exportPackingReportExcel?"
    

    导出复杂的word待续。。。。

    相关文章

      网友评论

          本文标题:easypoi 导出excel 和word文件

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