728x90
반응형
난 아래 처럼 구현 했는데
더보기
package com.rsn.POMS.api.lucy.action.service.excelComponent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractXlsxView;
import com.rsn.POMS.api.lucy.action.vo.ActionLogVo;
/**
* @author 정수빈
* 2021 05 12
*
* Action Log - 키워드 검색 현황
*/
@Component("actionLog.keywordStatus")
public class ActionKeywordStatus extends AbstractXlsxView {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
createExcel(workbook, model);
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyMMdd_HHmm");
Date time = new Date();
String date = dateFormat.format(time);
String fileName = "Lucy2.0_ActionKeywordStatus_"+date+".xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
try {
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
} catch (IOException e) {
throw new RuntimeException("IOException / 액셀 파일 생성 실패" + e.getMessage());
}
}
private final String[] Title = new String[] { "No", "검색 기록" };
private void createExcel(Workbook workbook, Map<String, Object> model) {
ExcelStyle style = new ExcelStyle();
CellStyle titleStyle = workbook.createCellStyle();
titleStyle = style.getTitlestyle(titleStyle);
Map<Integer, List<ActionLogVo>> excelDataMap = (Map<Integer, List<ActionLogVo>>) model.get("excelDataMap");
String sheetTitle = "";
for (int key : excelDataMap.keySet()) {
List<ActionLogVo> excelDataList = excelDataMap.get(key);
for (ActionLogVo logVo : excelDataList) {
sheetTitle = "검색로그_" + logVo.getUser_email() + "(" + logVo.getUser_name() + ")";
break;
}
Sheet sheet = workbook.createSheet(key + 1 + ")" + sheetTitle);
int rowIndex = 0;
Row row = null;
Cell cell = null;
row = sheet.createRow(rowIndex);
for (int i = 0; i < Title.length; i++) {
sheet.setColumnWidth(i, 5000);
cell = row.createCell(i);
cell.setCellValue(Title[i]);
cell.setCellStyle(titleStyle);
}
for (ActionLogVo logVo : excelDataList) {
row = sheet.createRow(++rowIndex);
cell = row.createCell(0);
cell.setCellValue(rowIndex);
cell = row.createCell(1);
cell.setCellValue(logVo.getSearch_data());
}
}
}
}
이게 잘못!
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
out 이를 닫아줘야 해
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
다영선배 TIP !
여기서 한가지 더,
아마 flush()를 선언해주고 close()를 선언하는 개발자가 있을것이다.
위 코드를 분석하면서 한가지 알아낸 사실은,
close() 하나만 선언해도 선언되지않은 flush() 메소드도 실행된다는 것이다.
근데 나는 삽질 중이었다.
삽질 전
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
createExcel(workbook, model);
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyMMdd_HHmm");
Date time = new Date();
String date = dateFormat.format(time);
String fileName = "Lucy2.0_ActionIpLoginStatus_"+date+".xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
try {
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException("IOException / 액셀 파일 생성 실패" + e.getMessage());
}
삽질 후
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
createExcel(workbook, model);
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyMMdd_HHmm");
Date time = new Date();
String date = dateFormat.format(time);
String fileName = "Lucy2.0_ActionIpLoginStatus_"+date+".xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
}
이거가 필요 없는 코드다!!!!!!!!!!!!!
try {
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException("IOException / 액셀 파일 생성 실패" + e.getMessage());
}
728x90
반응형
'개발중 > Java' 카테고리의 다른 글
LOMBOK - NoArgsConstructor/AllArgsConstructor 사용 (0) | 2021.05.14 |
---|---|
액셀 / 액셀 파일 한글 깨져요. (0) | 2021.05.13 |
lombok / getter setter 대소문자 호환 오류 (0) | 2021.05.13 |
액셀 / AbstractXlsxView - setContentType (0) | 2021.05.13 |
Excel 로딩바 -AbstractXlsxView (0) | 2021.05.06 |