Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法
上星期做了一個(gè)Excel的單元格合并,用的是EasyPoi,我之前合并單元格都是原生的,第一次使用EasyPoi合并也不太熟悉,看著網(wǎng)上自己套用,使用后發(fā)現(xiàn)比原生的方便些,貢獻(xiàn)一下,也給其他用到合并而且用的是EasyPoi的小伙伴節(jié)省下時(shí)間。
導(dǎo)出模板:版本號(hào),自己來(lái)定,可以去官網(wǎng)查看:EasyPoi官網(wǎng)
<!-- easypoi 導(dǎo)入包 --> <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>實(shí)現(xiàn)代碼:
//表頭設(shè)置List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();ExcelExportEntity colEntity = new ExcelExportEntity('經(jīng)銷商', '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('門店數(shù)量', '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('申請(qǐng)數(shù)量', 'applyNum' + i); appalyExcel.setStatistics(true); exportEntities.add(appalyExcel); ExcelExportEntity adoptExcel = new ExcelExportEntity('通過(guò)數(shù)量', 'adoptNum' + i); adoptExcel.setStatistics(true); exportEntities.add(adoptExcel); group_1.setList(exportEntities); colList.add(group_1);}//文件數(shù)據(jù)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);}//導(dǎo)出Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams('【' + request.getMonthStr() + '】門店注冊(cè)日明細(xì)數(shù)據(jù)', '數(shù)據(jù)'), colList, list);Sheet sheet = workbook.getSheet('數(shù)據(jù)');Row row = sheet.getRow(sheet.getLastRowNum());Cell cell = row.getCell(0);cell.setCellValue('總計(jì)');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('門店注冊(cè)日workbook寫入到文件中失敗,錯(cuò)誤信息:{}', ExceptionUtils.getStackTrace(e)); } finally { if (null != fileOutputStream) {try { fileOutputStream.close();} catch (IOException e) { //skip} } }
具體的API細(xì)節(jié)就不介紹了可以去官網(wǎng),關(guān)鍵在于ExcelExportEntity 這個(gè)類,它是以map形式展現(xiàn)的,創(chuàng)建的時(shí)候設(shè)置key,設(shè)置value的根據(jù)key進(jìn)行設(shè)置,上面一些StoreNewAddReportVO還有其他是我的業(yè)務(wù)類, 到時(shí)候可以替換掉。
到此這篇關(guān)于Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法的文章就介紹到這了,更多相關(guān)Java EasyPoi導(dǎo)出單元格內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. 如何通過(guò)vscode運(yùn)行調(diào)試javascript代碼3. HTML <!DOCTYPE> 標(biāo)簽4. JS數(shù)據(jù)類型判斷的幾種常用方法5. Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新留言效果6. WML語(yǔ)言的基本情況7. python基于tkinter制作無(wú)損音樂(lè)下載工具(附源碼)8. ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹形下拉框的方法9. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能10. 用Python實(shí)現(xiàn)定時(shí)備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器
