美文网首页
easypoi 多sheet导出

easypoi 多sheet导出

作者: 夜空最亮的9星 | 来源:发表于2019-12-11 15:16 被阅读0次
            <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>
    

    参考代码:

     public String export(){
    
            Workbook workBook = null;
            try {
    
    //
                List<Map<String, Object>> sheetsList = new ArrayList<>();
    
                List<DeptUtil> exportList = exportService.exportList();
    
                // 创建参数对象(用来设定excel得sheet得内容等信息)
                ExportParams deptExportParams = new ExportParams();
                // 设置sheet得名称
                deptExportParams.setSheetName("员工报表1");
                // 创建sheet1使用得map
                Map<String, Object> deptExportMap = new HashMap<>();
                // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
                deptExportMap.put("title", deptExportParams);
                // 模版导出对应得实体类型
                deptExportMap.put("entity", DeptUtil.class);
                // sheet中要填充得数据
                deptExportMap.put("data", exportList);
                sheetsList.add(deptExportMap);
    
                // 创建参数对象(用来设定excel得sheet得内容等信息)
                ExportParams empExportParams = new ExportParams();
                empExportParams.setSheetName("员工报表2");
                // 创建sheet2使用得map
                Map<String, Object> empExportMap = new HashMap<>();
                empExportMap.put("title", empExportParams);
                empExportMap.put("entity", DeptUtil.class);
                empExportMap.put("data", exportList);
                sheetsList.add(empExportMap);
    
    
    //             执行方法
                workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
                fileName = URLEncoder.encode("员工报表导出", "UTF-8");
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                workBook.write(outputStream);
                outputStream.flush();
                byte[] byteArray = outputStream.toByteArray();
                excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length);
                outputStream.close();
    
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(workBook != null) {
                    try {
                        workBook.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return "success";
        }
    

    官方文档:http://easypoi.mydoc.io/

    相关文章

      网友评论

          本文标题:easypoi 多sheet导出

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