Spring
Apache poi 4.0.x 라이브러리 Spring excel 다운로드 (기본)
猫猫
2020. 7. 14. 15:09
반응형
jsp (script에서 버튼(id 지정)에 이벤트 부여)
// 엑셀 다운로드 버튼 클릭
$('#btnExcelDown').click(function () {
let searchForm = setBeforeSubmit();
if (confirm("다운로드 하시겠습니까?") == false) {
return;
}
if (!searchForm) {
alert("일시적인 오류입니다. 새로고침 후 재시도하시기 바랍니다.");
return;
}
location.href = "/emp/excelDown?" + searchForm;
});
controller -일단 테스트용이라 그냥 넘길거임. responseEntity<String> 은 스트링형태로 넘기려 하는것이니 리턴형은 알아서.
@RequestMapping("/excelDown")
public ResponseEntity<String> excelDown(HttpServletRequest request, Model model) throws Exception {
model.addAttribute("request", request);
return CommonUtil.createResponseEntity(empExcelDownService, model);
}
service - 가장 간단한 틀
@Service
public class EmpExcelDownService implements IService {
@Override
public String execute(Model model) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sheet");
sheet.setColumnWidth(0, 2560);
sheet.setColumnWidth(1, 2560);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello");
File file = new File("C:\\Users\\user\\Desktop\\excel.xlsx");
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file, false);
wb.write(outputStream);
outputStream.close();
return null;
}
}
jsp버튼 누르면 컨트롤러 -> 서비스로 넘어와서 excel 파일 다운로드 된다. file의 path에 바탕화면이라고 저장해놔서
바탕화면에 excel.xlsx 가 보일것.
순서대로 엑셀 workbook -> sheet -> row -> cell -> cell에 값 을 넣는것.
기본틀 설명이 별로 없어서 고생함
반응형