POI 라이브러리를 사용한 대용량 데이터 Excel 다운로드 소스 정리
private static final int THREAD_POOL_SIZE = 10;
@Transactional(readOnly = true)
public void oralHealthRiskAssessmentDownload(/*String token*/) throws InterruptedException, IOException {
List<Entity조회> list = entity조회서비스.listAll();
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("시트명");
ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
for (int i=0; i< list.size(); i++) {
int index = i;
executorService.execute(() -> {
synchronized (sheet) {
Entity entity = list.get(index);
Row row = sheet.createRow(index); // -> 만약 순서 상관 없으면 Row row = sheet.getLastRowNum() + 1; 사용
int cellIndex = 0;
Cell idCell = row.createCell(cellIndex++);
idCell.setCellValue(entity.getId());
Cell nameCell = row.createCell(cellIndex);
nameCell.setCellValue(entity.getName());
}
});
}
executorService.shutdown();
//noinspection ResultOfMethodCallIgnored
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
FileOutputStream outputStream = new FileOutputStream("test.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
'Backend > Spring' 카테고리의 다른 글
Spring 3.0 Rest Docs requestParameters Removed (0) | 2023.08.04 |
---|---|
[GS인증] 데이터 암호화 처리 > AES-128 알고리즘 (0) | 2023.07.25 |
POI -> 대용량 데이터 Excel 업로드 및 DB 저장 (Batch 사용) (0) | 2023.02.28 |
Caused by: java.lang.ClassNotFoundException: Could not load requested class : json (0) | 2022.03.03 |
Spring Boot - (5) AOP 설정 (0) | 2020.11.26 |