자바 에서 Easypoi 는 엑셀 다 중 시트 가 져 오기 내 보 내기 기능 을 실현 합 니 다.

Easypoi 는 개발 중 문서 가 져 오기 내 보 내기 실현 을 간소화 하 였 으 며,poi 처럼 큰 도구 류 를 써 서 문서 의 읽 기와 쓰 기 를 해결 해 야 하 는 것 은 아 닙 니 다.
Easypoi 의존 도입

 <!--        EasyPoi  Excel         -->
  <dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-spring-boot-starter</artifactId>
   <version>4.2.0</version>
  </dependency>
Easypoi 의 주석 사용 설명(남 겨 두 고 보면 됩 니 다)

두 번 째 단 계 는 표 헤드 데이터 대상 의 실체 류 를 정의 합 니 다.(주해 의 사용 은 위의 필요 에 따라 사용 하면 됩 니 다)
sheet
sheet2

@Setter
@Getter
@ToString
public class LoginCaseDto {
 @Excel(name = "flag(0   ,1   )",orderNum = "1",width = 20)
 private String flag;
 @Excel(name = "urlid(  id)",orderNum = "2",width = 20)
 private String urlid;
 @Excel(name = "name(    )",orderNum = "3",width = 20)
 private String name;
 @Excel(name = "pwd(    )",orderNum = "4",width = 20)
 private String pwd;
 @Excel(name = "desc(     )",orderNum = "5",width = 40)
 private String desc;
 @Excel(name = "actual(      )",orderNum = "6",width = 40 )
 private String actual;
 @Excel(name = "urlpath(    )",orderNum = "7",width = 40 )
 private String urlpath;
}

public class LoginUrlDto {
 @Excel(name = "id(      )",orderNum = "1",width = 20)
 private String id;
 @Excel(name = "type(    )",orderNum = "2",width = 20)
 private String type;
 @Excel(name = "url(    )",orderNum = "3",width = 40)
 private String url;
}
세 번 째 단계:Easypoi 도구 류(인터넷 에서 많이 찾 았 지만 완전 하지 않 습 니 다.여기 서 보충 합 니 다)
참고 문장
키 패키지 도구 클래스 다 중 sheet 가 져 오기 방법

 /**
  *     :     Excel       sheet,           
  * @param filePath       
  * @param sheetIndex   sheet  
  * @param titleRows       
  * @param headerRows     
  * @param pojoClass Excel   
  * @return
  */
 public static <T> List<T> importExcel(String filePath,int sheetIndex,Integer titleRows, Integer headerRows, Class<T> pojoClass) {
  //   file  Workbook,            ,    excel   sheet 
  ImportParams params = new ImportParams();
  //    sheet 
  params.setStartSheetIndex(sheetIndex);
  params.setTitleRows(titleRows);
  params.setHeadRows(headerRows);
  List<T> list = null;
  try {
   list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  } catch (NoSuchElementException e) {
   throw new RuntimeException("      ");
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }
excel 예제 가 져 오기(sheet 색인 에 직접 전송 하여 해당 하 는 sheet 표를 가 져 옵 니 다)

다 중 sheet 표 내 보 내기 방법 사용(가 져 온 다 중 sheet 표 데 이 터 를 list 집합 으로 변환 하여 새 데 이 터 를 가 져 온 후 이 방법 으로 다시 기록 해 야 합 니 다)

 /**
  *     :        sheet        ,        List<Map<String, Object>>  ,        
  * @ExcelEntiry sheet         
  * @return
  */
 public static String exportSheet( Object...objects){
  Workbook workBook = null;
  try {
   //       (    excel sheet      )
   ExportParams deptExportParams = new ExportParams();
   //   sheet   
   deptExportParams.setSheetName("    ");
   //   sheet    
   deptExportParams.setTitle("    ");
   //   sheet1   map
   Map<String, Object> deptExportMap = new HashMap<>();
   // title    ExportParams  ,     ExportParams    sheetName
   deptExportMap.put("title", deptExportParams);
   //            
   deptExportMap.put("entity", LoginCaseDto.class);
   // sheet       
   deptExportMap.put("data", objects[0]);
   ExportParams empExportParams = new ExportParams();
   empExportParams.setTitle("  RUL  ");
   empExportParams.setSheetName("  url");
   //   sheet2   map
   Map<String, Object> empExportMap = new HashMap<>();
   empExportMap.put("title", empExportParams);
   empExportMap.put("entity", LoginUrlDto.class);
   empExportMap.put("data", objects[1]);
   //  sheet1、sheet2   map    
   List<Map<String, Object>> sheetsList = new ArrayList<>();
   sheetsList.add(deptExportMap);
   sheetsList.add(empExportMap);
   //     
   workBook = EasyPoiUtil.exportExcel(sheetsList, ExcelType.HSSF);
   //String fileName = URLEncoder.encode("test", "UTF-8");
   String filepath = (String) LoadStaticConfigUtil.getCommonYml( "testcaseexcel.cases");
   FileOutputStream fos = new FileOutputStream(filepath);
   workBook.write(fos);
   fos.close();
  }catch (Exception e){
   e.printStackTrace();
  }finally {
   if(workBook != null) {
    try {
     workBook.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return "success";
 }
마지막 으로 새로운 테스트 결과 표를 얻 을 수 있 습 니 다.
프로젝트 원본 주소 전송 문
자바 에서 Easypoi 가 엑셀 다 중 sheet 표 가 져 오기 내 보 내기 기능 을 실현 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 Easypoi 엑셀 다 중 sheet 표 가 져 오기 내 보 내기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기