您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在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導出復雜的單元格,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。