Java 에서 EasyPoi 멀 티 시트 내 보 내기 기능 구현
머리말:블 로 그 를 시작 하려 고 했 는데 움 직 일 시간 이 없 었 어 요.오늘 드디어 제 첫 블 로 그 를 시 작 했 어 요.
최근 에 엑셀 기능 을 내 보 내 는 수 요 를 받 았 는데 이 기능 의 주요 난점 은?
말 이 많 지 않 습 니 다.다음 에 제 코드 를 공유 하 겠 습 니 다.(저 는 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 멀 티 시트 내 보 내기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.