[Java] 엑셀 다운로드 기능 구현!! (영상 有)

[Java] 엑셀 다운로드 기능 구현!! (영상 有)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

private void setHeaderCS(CellStyle cs, Font font, Cell cell) { cs.setAlignment(CellStyle.ALIGN_CENTER); cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setFillForegroundColor(HSSFColor.GREY_80_PERCENT.index); cs.setFillPattern(CellStyle.SOLID_FOREGROUND); setHeaderFont(font, cell); cs.setFont(font); cell.setCellStyle(cs); } private void setHeaderFont(Font font, Cell cell) { font.setBoldweight(( short ) 700 ); font.setColor(HSSFColor.WHITE.index); } private void setCmmnCS2(CellStyle cs, Cell cell) { cs.setAlignment(CellStyle.ALIGN_LEFT); cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cell.setCellStyle(cs); } @Override public void getReserveExcel(tbl_reserveVO searchVO, HttpServletRequest request, HttpServletResponse response) throws Exception { List < tbl_reserveVO > list = dao.getReserveExcel(searchVO); SXSSFWorkbook wb = new SXSSFWorkbook(); Sheet sheet = wb.createSheet(); sheet.setColumnWidth(( short ) 0 , ( short ) 2000 ); sheet.setColumnWidth(( short ) 1 , ( short ) 8000 ); sheet.setColumnWidth(( short ) 2 , ( short ) 3000 ); sheet.setColumnWidth(( short ) 3 , ( short ) 3000 ); sheet.setColumnWidth(( short ) 4 , ( short ) 8000 ); sheet.setColumnWidth(( short ) 5 , ( short ) 5000 ); sheet.setColumnWidth(( short ) 6 , ( short ) 3000 ); Row row = sheet.createRow( 0 ); Cell cell = null ; CellStyle cs = wb.createCellStyle(); Font font = wb.createFont(); cell = row.createCell( 0 ); cell.setCellValue( "예약신청 관리 - 예약신청 리스트" ); setHeaderCS(cs, font, cell); sheet.addMergedRegion( new CellRangeAddress(row.getRowNum(), row.getRowNum(), 0 , 6 )); row = sheet.createRow( 1 ); cell = null ; cs = wb.createCellStyle(); font = wb.createFont(); cell = row.createCell( 0 ); cell.setCellValue( "번호" ); setHeaderCS(cs, font, cell); cell = row.createCell( 1 ); cell.setCellValue( "이름" ); setHeaderCS(cs, font, cell); cell = row.createCell( 2 ); cell.setCellValue( "전화번호" ); setHeaderCS(cs, font, cell); cell = row.createCell( 3 ); cell.setCellValue( "관람일자" ); setHeaderCS(cs, font, cell); cell = row.createCell( 4 ); cell.setCellValue( "프로그램명" ); setHeaderCS(cs, font, cell); cell = row.createCell( 5 ); cell.setCellValue( "방문인원" ); setHeaderCS(cs, font, cell); cell = row.createCell( 6 ); cell.setCellValue( "예약신청일" ); setHeaderCS(cs, font, cell); int i = 2 ; int ii = list.size(); for (tbl_reserveVO vo : list) { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); String cretDate = sdf. format (vo.getCret_date()); row = sheet.createRow(i); cell = null ; cs = wb.createCellStyle(); font = wb.createFont(); cell = row.createCell( 0 ); cell.setCellValue(ii); setCmmnCS2(cs, cell); cell = row.createCell( 1 ); cell.setCellValue(vo.getResv_name()); setCmmnCS2(cs, cell); cell = row.createCell( 2 ); cell.setCellValue(vo.getResv_tel()); setCmmnCS2(cs, cell); cell = row.createCell( 3 ); cell.setCellValue(vo.getResv_day_name()); setCmmnCS2(cs, cell); cell = row.createCell( 4 ); cell.setCellValue(vo.getResv_program_name()); setCmmnCS2(cs, cell); cell = row.createCell( 5 ); cell.setCellValue(vo.getResv_number()); setCmmnCS2(cs, cell); cell = row.createCell( 6 ); cell.setCellValue(cretDate); setCmmnCS2(cs, cell); i + + ; ii - - ; } response.setHeader( "Set-Cookie" , "fileDownload=true; path=/" ); response.setHeader( "Content-Disposition" , String . format ( "attachment; filename=\"ReserveManageList.xlsx\"" )); wb.write(response.getOutputStream()); } Colored by Color Scripter

from http://chobopark.tistory.com/233 by ccl(A) rewrite - 2021-11-27 10:02:09