자바 에서 EasyPoi 에서 복잡 한 병합 셀 내 보 내기

선언:
지난주 에 Excel 의 셀 합병 을 했 는데 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 에 따라 설정 합 니 다.위의 Store NewAddReportVO 와 다른 것 은 제 업무 유형 이 고 때 가 되면 교체 할 수 있 습 니 다.
자바 에서 EasyPoi 가 복잡 한 병합 셀 을 내 보 내 는 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 EasyPoi 내 보 내기 셀 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기