Java中EasyPoi導出復雜合并單元格的方法
上星期做了一個Excel的單元格合并,用的是EasyPoi,我之前合并單元格都是原生的,第一次使用EasyPoi合并也不太熟悉,看著網上自己套用,使用后發現比原生的方便些,貢獻一下,也給其他用到合并而且用的是EasyPoi的小伙伴節省下時間。
導出模板:版本號,自己來定,可以去官網查看: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} } }
具體的API細節就不介紹了可以去官網,關鍵在于ExcelExportEntity 這個類,它是以map形式展現的,創建的時候設置key,設置value的根據key進行設置,上面一些StoreNewAddReportVO還有其他是我的業務類, 到時候可以替換掉。
到此這篇關于Java中EasyPoi導出復雜合并單元格的方法的文章就介紹到這了,更多相關Java EasyPoi導出單元格內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. Intellij IDEA官方最完美編程字體Mono使用2. springboot基于Redis發布訂閱集群下WebSocket的解決方案3. 關于探究python中sys.argv時遇到的問題詳解4. 基于android studio的layout的xml文件的創建方式5. CSS自定義滾動條樣式案例詳解6. JS繪圖Flot如何實現動態可刷新曲線圖7. IDEA項目的依賴(pom.xml文件)導入問題及解決8. python使用requests庫爬取拉勾網招聘信息的實現9. 使用ProcessBuilder調用外部命令,并返回大量結果10. Java發送http請求的示例(get與post方法請求)
