溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Java中利用EasyPoi導(dǎo)出復(fù)雜的單元格

發(fā)布時(shí)間:2021-01-14 14:15:41 來源:億速云 閱讀:494 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了怎么在Java中利用EasyPoi導(dǎo)出復(fù)雜的單元格,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

<!-- 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("通過數(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
		        }
		      }
		    }

上述內(nèi)容就是怎么在Java中利用EasyPoi導(dǎo)出復(fù)雜的單元格,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI