温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Java中利用EasyPoi导出复杂的单元格

发布时间:2021-01-14 14:15:41 来源:亿速云 阅读:551 作者:Leah 栏目:开发技术

本篇文章为大家展示了怎么在Java中利用EasyPoi导出复杂的单元格,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<!-- easypoi 导入包 -->     <dependency>       <groupId>cn.afterturn</groupId>       <artifactId>easypoi-base</artifactId>       <version>4.0.0</version>     </dependency>     <dependency>       <groupId>cn.afterturn</groupId>       <artifactId>easypoi-annotation</artifactId>       <version>4.0.0</version>     </dependency>

实现代码:

 //表头设置         List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();         ExcelExportEntity colEntity = new ExcelExportEntity("经销商", "distributorName");         colEntity.setNeedMerge(true);         colEntity.setWidth(20);         colList.add(colEntity);         colEntity = new ExcelExportEntity("科室", "dept");         colEntity.setNeedMerge(true);         colList.add(colEntity);         colEntity = new ExcelExportEntity("部门", "region");         colEntity.setNeedMerge(true);         colList.add(colEntity);         colEntity = new ExcelExportEntity("省份", "province");         colEntity.setNeedMerge(true);         colList.add(colEntity);         colEntity = new ExcelExportEntity("门店数量", "storeNum");         colEntity.setNeedMerge(true);         colEntity.setStatistics(true);         colList.add(colEntity);         Map<String, Integer> map = DateUtils.getLastDayOfMonthByStr(request.getMonthStr());         Integer dayNum = map.get("dayNum");         for (int i = 1; i <= dayNum; i++) {           ExcelExportEntity group_1 = new ExcelExportEntity(i + "日", "day");           List<ExcelExportEntity> exportEntities = new ArrayList<>();           ExcelExportEntity appalyExcel = new ExcelExportEntity("申请数量", "applyNum" + i);           appalyExcel.setStatistics(true);           exportEntities.add(appalyExcel);           ExcelExportEntity adoptExcel = new ExcelExportEntity("通过数量", "adoptNum" + i);           adoptExcel.setStatistics(true);           exportEntities.add(adoptExcel);           group_1.setList(exportEntities);           colList.add(group_1);         }         //文件数据         List<Map<String, Object>> list = new ArrayList<>();         List<StoreNewAddReportVO.DistributorStoreNewAddReportVO> disList = register.getStoreNewAddReportVO().getDistributorStoreNewAddReportVOList();         int size = disList.size();         for (int i = 0; i < size; i++) {           StoreNewAddReportVO.DistributorStoreNewAddReportVO dis = disList.get(i);           Map<String, Object> valMap = new HashMap<>();           valMap.put("distributorName", dis.getDistributorName());           valMap.put("dept", dis.getDept());           valMap.put("region", dis.getRegion());           valMap.put("province", dis.getProvince());           valMap.put("storeNum", dis.getStoreNum());           List<StoreNewAddReportVO.dayData> dayDataList = dis.getDayDataList();           Map<String, List<StoreNewAddReportVO.dayData>> collectMap = Maps.newHashMap();           if (CollectionUtils.isNotEmpty(dayDataList)) {             collectMap = dayDataList.stream().collect(Collectors.groupingBy(StoreNewAddReportVO.dayData::getDayStr));           }           List<Map<String, Object>> list_1 = new ArrayList<>();           Map<String, Object> valMap_1 = new HashMap<>();           for (int j = 1; j <= dayNum; j++) {             List<StoreNewAddReportVO.dayData> dayData = collectMap.get(String.valueOf(j));             int applyflag = 0;             int adoptflag = 0;             if (CollectionUtils.isNotEmpty(dayData)) {               applyflag = dayData.get(0).getApplyNum();               adoptflag = dayData.get(0).getAdoptNum();             }             valMap_1.put("applyNum" + j, applyflag);             valMap_1.put("adoptNum" + j, adoptflag);           }           list_1.add(valMap_1);           valMap.put("day", list_1);           list.add(valMap);         }         //导出         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("【" + request.getMonthStr() + "】门店注册日明细数据", "数据"), colList, list);         Sheet sheet = workbook.getSheet("数据");         Row row = sheet.getRow(sheet.getLastRowNum());         Cell cell = row.getCell(0);         cell.setCellValue("总计");         CellStyle cellStyle = workbook.createCellStyle();         cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中         cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中         Font font = workbook.createFont();         font.setFontHeightInPoints((short) 15);         font.setFontName("Trebuchet MS");         cellStyle.setFont(font);         cell.setCellStyle(cellStyle);         CellRangeAddress range_0 = new CellRangeAddress(sheet.getLastRowNum(), sheet.getLastRowNum(), 0, 3);         sheet.addMergedRegion(range_0);         File file = new File("D:\\".concat(UUID.randomUUID().toString().concat(".xls")));	    FileOutputStream fileOutputStream = null;	    try {	      fileOutputStream = new FileOutputStream(file);	      workbook.write(fileOutputStream);	    } catch (Exception e) {	      log.error("门店注册日workbook写入到文件中失败,错误信息:{}", ExceptionUtils.getStackTrace(e));	    } finally {	      if (null != fileOutputStream) {	        try {	          fileOutputStream.close();	        } catch (IOException e) {	          //skip	        }	      }	    }

上述内容就是怎么在Java中利用EasyPoi导出复杂的单元格,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI