Java 에서 EasyPoi 멀 티 시트 내 보 내기 기능 구현

EasyPoi 멀 티 시트 내 보 내기
머리말:블 로 그 를 시작 하려 고 했 는데 움 직 일 시간 이 없 었 어 요.오늘 드디어 제 첫 블 로 그 를 시 작 했 어 요.
최근 에 엑셀 기능 을 내 보 내 는 수 요 를 받 았 는데 이 기능 의 주요 난점 은?
  • 멀 티 시트
  • 병합 셀 내 보 내기(행 간,열 간)
  • 다 중 헤더 합병
  • 제 가 시작 한 생각 은 poi 를 사용 하여 이 기능 을 실현 하면 업무 논리 가 좀 복잡 할 수 있다 는 것 입 니 다.그래서 저 는 easy Poi―so easy 도 구 를 사 용 했 습 니 다.그 특징 은 매우 편리 하 다 는 것 입 니 다.jQuery 의 한 마디 로 write Less,Do More 입 니 다.
    말 이 많 지 않 습 니 다.다음 에 제 코드 를 공유 하 겠 습 니 다.(저 는 SSH 프레임 워 크 구축 환경 을 사용 합 니 다)
    1.maven jar 가방 도입
    
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>3.2.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>3.2.0</version>
    </dependency>
    2.내 보 낼 실체 클래스 를 작성 합 니 다(주석 형식 사용)
    1.DeptUtil 류
    
    @ExcelTarget("deptUtil")
    public class DeptUtil {
    
      @Excel(name = "    ", width = 30 , needMerge = true)
      private Integer id;
    
      @Excel(name = "    ", width = 30 , needMerge = true)
      private String deptName;
    
      @ExcelCollection(name = "    ")
      private List<EmpUtil> emps;
    
    
    	....  getter、setter  
    2.EmpUtil 류
    
    @ExcelTarget("empUtil")
    public class EmpUtil{
    
      @Excel(name = "  ", width = 30, isColumnHidden = true)
      private Integer id;
    
      @Excel(name = "    ", width = 30, groupName = "    ")
      private String empName;
    
      @Excel(name = "  ", width = 30, type = 10, groupName = "    ")
      private Integer age;
    
      @Excel(name = "    ", width = 30, groupName = "    ", format = "yyyy/MM/dd HH:mm")
      private Date hiredate;
    
      @Excel(name = "  ", width = 30, type = 10, groupName = "    ")
      private BigDecimal salary;
    
    
    	....  getter、setter  
    3.핵심 코드
    
    public String export(){
    
        Workbook workBook = null;
        try {
          List<DeptUtil> exportList = exportService.exportList();
          System.err.println(JSONArray.toJSONString(exportList));
    
          //       (    excel sheet      )
          ExportParams deptExportParams = new ExportParams();
          //   sheet   
          deptExportParams.setSheetName("    1");
          //   sheet1   map
          Map<String, Object> deptExportMap = new HashMap<>();
          // title    ExportParams  ,     ExportParams    sheetName
          deptExportMap.put("title", deptExportParams);
          //            
          deptExportMap.put("entity", DeptUtil.class);
          // sheet       
          deptExportMap.put("data", exportList);
    
          ExportParams empExportParams = new ExportParams();
          empExportParams.setSheetName("    2");
          //   sheet2   map
          Map<String, Object> empExportMap = new HashMap<>();
          empExportMap.put("title", empExportParams);
          empExportMap.put("entity", DeptUtil.class);
          empExportMap.put("data", exportList);
    
          //  sheet1、sheet2、sheet3   map    
          List<Map<String, Object>> sheetsList = new ArrayList<>();
          sheetsList.add(deptExportMap);
          sheetsList.add(empExportMap);
          //     
          workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
          fileName = URLEncoder.encode("      ", "UTF-8");
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          workBook.write(outputStream);
          outputStream.flush();
          byte[] byteArray = outputStream.toByteArray();
          excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length);
          outputStream.close();
    
        }catch (Exception e){
          e.printStackTrace();
        }finally {
          if(workBook != null) {
            try {
              workBook.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
        return "success";
      }
    3.효과 전시
    在这里插入图片描述
    문 제 는 sheet 가 채 워 진 데이터 원본 이 같 으 면 두 번 째 sheet 의 내용 이 비어 있다 는 것 이다.
    在这里插入图片描述
    그러나 일반적으로 다 중 sheet 페이지 전 시 를 실현 하려 면 내용 이 다 를 것 입 니 다.여 기 는 실현 효과 전시 로 서 같은 데이터 획득 원 을 사용 합 니 다.
    이상 은 모든 내용 입 니 다.더 배우 고 싶 으 면 easyPoi 의 apihttp://easypoi.mydoc.io/를 찾 아 보 세 요.
    그리고 제 프로젝트 소스 코드 를 동봉 합 니 다.https://github.com/wzqonly/easyPoi_export
    자바 의 EasyPoi 멀 티 시트 내 보 내기 기능 구현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 EasyPoi 멀 티 시트 내 보 내기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

    좋은 웹페이지 즐겨찾기