Published 2019. 11. 28. 13:48

controller

/* 엑셀 다운로드 */
	
	@GetMapping(value={ STATISTICS.STATISTICS_EXCEL_DOWNLOAD })
	public void getStatisticsExcel(@ModelAttribute StatisticsInfo statisticsInfo, HttpServletRequest req, HttpServletResponse res, Principal principal) throws Exception {
	
		String mbrId = principal.getName();
		
		if(mbrId == null) {
			mbrId = "";
		}
		statisticsInfo.setMbrId(mbrId);
		
		/** 통계 리스트 조회 */
		List<StatisticsInfo> list = statisticsService.selectStatisticsList(statisticsInfo);
		
		// 메모리에 100개의 행을 유지합니다. 행의 수가 넘으면 디스크에 적습니다.
		SXSSFWorkbook wb = new SXSSFWorkbook(100);
		Sheet sheet = wb.createSheet();
	    Row row = null;
	    int rowNo = 0;
	    Cell cell = null;
	    
	    for(int k=0; k<list.size(); k++) {
	    	sheet.setColumnWidth(k, 21*256);
	    }
	    
//	    // 테이블 헤더용 스타일
	    CellStyle headStyle = wb.createCellStyle();
	   
	    // 배경색은 노란색입니다.
	    headStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
	    headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
	    headStyle.setBorderTop(CellStyle.BORDER_THIN);
	    headStyle.setBorderBottom(CellStyle.BORDER_THIN);
	    headStyle.setBorderLeft(CellStyle.BORDER_THIN);
	    headStyle.setBorderRight(CellStyle.BORDER_THIN);
	    
//	    // 데이터용 경계 스타일 테두리만 지정
	    CellStyle bodyStyle = wb.createCellStyle();
	    
	    bodyStyle.setAlignment(bodyStyle.ALIGN_CENTER);
	    bodyStyle.setBorderTop(CellStyle.BORDER_THIN);
	    bodyStyle.setBorderBottom(CellStyle.BORDER_THIN);
	    bodyStyle.setBorderLeft(CellStyle.BORDER_THIN);
	    bodyStyle.setBorderRight(CellStyle.BORDER_THIN);
	    
		try {
			row = sheet.createRow(rowNo++);
		    cell = row.createCell(0);
		    cell.setCellStyle(headStyle);
		    cell.setCellValue("ID");
		    
		    cell = row.createCell(1);
		    cell.setCellStyle(headStyle);
		    cell.setCellValue("接続IP");
		    
		    cell = row.createCell(2);
		    cell.setCellStyle(headStyle);
		    cell.setCellValue("接続日");
		    
		    cell = row.createCell(3);
		    cell.setCellStyle(headStyle);
		    cell.setCellValue("接続環境");
		    
		    cell = row.createCell(4);
		    cell.setCellStyle(headStyle);
		    cell.setCellValue("接続ブラウザ");
		    
//		    //데이터 부분 생성
		    for(StatisticsInfo vo : list) {
		    	row = sheet.createRow(rowNo++);
		        cell = row.createCell(0);
		        cell.setCellStyle(bodyStyle);
		        cell.setCellValue(vo.getUid());
		        
		        cell = row.createCell(1);
		        cell.setCellStyle(bodyStyle);
		        cell.setCellValue(vo.getConIp());
		        
		        cell = row.createCell(2);
		        cell.setCellStyle(bodyStyle);
		        cell.setCellValue(vo.getCreateTime());
			        
		        cell = row.createCell(3);
		        cell.setCellStyle(bodyStyle);
		        cell.setCellValue(vo.getDeviceType());
			       
		        cell = row.createCell(4);
		        cell.setCellStyle(bodyStyle);
		        cell.setCellValue(vo.getBrowserName());
		    }
		    
		    res.setHeader("Set-Cookie", "fileDownload=true; path=/");
		    res.setHeader("Content-Disposition", String.format("attachment; filename=\"statistics.xlsx\""));
			wb.write(res.getOutputStream());

		} catch(Exception e) {
			res.setHeader("Set-Cookie", "fileDownload=false; path=/");
			res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
			res.setHeader("Content-Type","text/html; charset=utf-8");
			
			OutputStream out = null;
			try {
				out = res.getOutputStream();
				byte[] data = new String("fail..").getBytes();
				out.write(data, 0, data.length);
			} catch(Exception ignore) {
				ignore.printStackTrace();
			} finally {
				if(out != null) try { out.close(); } catch(Exception ignore) {}
			}
			
		} finally {

			// 디스크 적었던 임시파일을 제거합니다.
			wb.dispose();
			
			try { res.getOutputStream().close(); } catch(Exception ignore) {}
		}
		
		
	}

jsp

location.href = "${ct:url('STATISTICS.STATISTICS_EXCEL_DOWNLOAD')}?accKey="+accKey+"&schStartDate="+schStartDate+"&schEndDate="+schEndDate;
복사했습니다!