[기존]
.jsp에서 ExcelForm을 따로 만들고 스크립트에서 ajax를 사용하여 Json형식으로 Contoller에서 데이터를 불러온 후에 성공하면 ExcelController에 data를 던져서 다운로드를 구현하였다.
>>
[변경]
기존 방식으로 Excel 다운로드를 구현하려고 할 때, Ajax 요청을 사용하여 데이터를 불러온 후 , 데이터를 <form>을 통해 다시 excelController에 전달하는 방식은 소규모데이터에 적합할 수 있지만 대용량 데이터를 처리할 때는 몇가지 비효율성이 발생할 수 있다.
*Json은 대용량 데이터를 처리하기에 적합하지 않음
문제점
1. 브라우저 메모리 사용 증가
- JSON 데이터를 불러오면 모든 데이터가 브라우저 메모리에 저장된다. 대용량 데이터를 JSON형식으로 다루면, 클라이언트 메모리를 과도하게 사용하여 브라우저 성능이 느려지거나 멈출 수 있다.
2. 불필요한 데이터 전송 오버헤드
- Ajax를 통해 가져온 JSON 데이터를 다시 <form>을 통해 서버로 전송하는 방식은 데이터 전송이 중복되는셈이다.
3. 파일 생성 지연
- 대용량 데이터를 클라이언트에 불러오고 다시 서버로 전송하는 동안, 파일 생성 요청이 지연되므로 파일 다운로드 속도가 느려진다.
개선된 방식의 장점
ExcelController에서 직접 데이터를 불러와 서버에서 바로 엑셀 파일을 생성하는 방식은 여러 장점이 있다.
1. 서버에서 직접 데이터 처리
- Controller가 데이터베이스에서 대량 데이터를 직접 가쟈와서 파일을 생성하면, 불필요한 클라이언트-서버 간 데이터 전송이 사라지므로 속도와 효율이 개선된다.
2. 즉시 파일 생성 및 다운로드 기능
- 데이터를 받아오는 즉시 서버에서 엑셀 파일을 생성하여 다운로드하므로, 전체 다운로드 과정이 훨씬 간단하고 빠르게 작동하게된다.
3. 네트워크 비용의 절감
- 대량 데이터의 경우, 네트워크 트래픽과 클라이언트 부하가 적어지면서 네트워크 비용이 절감된다.
.jsp
<a class="btns btn-back-o" id="excelDownloadBtn"><span>엑셀다운</span></a>
<script>
$(document).on('click','#excelDownloadBtn',function() {
$(form).attr("action","/excel/log");
$(form).attr("method","post")
$(form).submit();
$(form).attr("action","/qr/log");
$(form).attr("method","get")
});
</script>
ExcelController
@Logging(custom = true, action = "요청", menuNm = "로그 엑셀다운로드", success = true, recodingParameter = true)
@PostMapping("/qr/log")
public ModelAndView qrLogExcelDownload(SearchVO searchVO) throws Exception {
Map<String, Object> map = new HashMap<>();
List<LogVO> list = service.getLogAllList(searchVO);
map.put("list", list);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
String data = json;
ExcelManager excelManager = new ExcelManager();
String templateExcelfilePath = "";
String makeExcelFilePath = "";
try {
templateExcelfilePath = resourceLoader.getResource("classpath:/excel/logSampleExcel.xlsx").getURI().getPath();
makeExcelFilePath = FILE_DOWNLOAD_TEMP + File.separator + UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx";
excelManager.makeExcel(templateExcelfilePath, makeExcelFilePath, data, 5);
// 마지막 부분에서 셀 갯수 지정 (0부터 시작)
} catch (IOException e) {
log.error("엑셀 파일 로드 실패 {}", e);
} catch (ExcelCommentNotFountException e) {
log.error("엑셀 다운로드 실패 {}", e);
}
return new ModelAndView("downloadView", "downloadFile", new File(makeExcelFilePath)).addObject("fileName", "로그.xlsx");
}
'Develop > Spring' 카테고리의 다른 글
[Springboot] Gmail SMTP 서버 연결하기 (1) | 2025.03.07 |
---|---|
[Spring]빌드도구 Maven과 Gradle의 차이 (0) | 2024.10.18 |
[Spring]Filter와 Interceptor의 개념 및 차이 (0) | 2024.10.17 |
[Spring] Validator의 주요개념과 적용 방법 (0) | 2024.10.02 |
@RequestBody와 @ModelAttribute (0) | 2024.08.01 |