excel down load 기능을 구현한 api는 많을지 모르지만 검색했을때는 poi를 많이 사용하고 있었고.
현재 내가 다니는 회사도 poi로 구축되어 있는데 하다보니 간단한 부분은 정리해두면 좋을듯 하여 적어본다.
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 | int iRow = 0; // 세로출 정의 int iCol = 0; // 가로줄 정의 Map params = parameters.getMap(); Map<String, String> testList = "DB QUERY LIST"; private String excel_location = PmisConfig.get("excel.template.home"); //WEB-INF 쪽에 EXCEL TEMPLATE경로 지정 String tmphome = Strings.defaultValue(excel_location, context.getRealPath("excel 양식경로")); String file_name = Strings.defaultValue(params.get("file_nm"), "FILE명"); // FileInputStream fis = new FileInputStream(tmphome + "/" + file_name); XSSFWorkbook book = new XSSFWorkbook(fis); XSSFSheet sheet = book.getSheetAt(0); //EXCEL 시트 지정 Row row = null; Cell cell = null; /*font style :엑셀 필요 시트에 사용할 STYLE 지정 */ Font font = book.createFont(); font.setFontHeightInPoints((short)8); font.setFontName("맑은 고딕"); XSSFCellStyle cellStyle = book.createCellStyle(); //엑셀에서 사용할 스타일명 지정 XSSFDataFormat fmt = book.createDataFormat(); /*해당 지정한 스타일에 border 나 배경색 위치정도를 지정해준다.*/ cellStyle.setFont(font); cellStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT); //text align 설정 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); //border 그려줄 부분 지정 cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); //배경색 지정(Back Ground color ) cellStyle.setWrapText(false); /*data 가져온훈 필요한 부분을 형변한 해준다. data type*/ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //날짜 SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy.MM"); DecimalFormat numberFormat = new DecimalFormat("###,###,###.0");// 금액관련 소수점 처리 DecimalFormat numberFormat2 = new DecimalFormat("###,###"); Calendar c = Calendar.getInstance(); | cs |
위의 코드는
excel 구현하기 위한 재료라고 생각하면 될듯하다.
1. sheet 순서
2. excel template 경로
3. excel에서 사용할 font 및 css 정도
이젠 엑셀을 그려보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //그리기 시작할 ROW를 선언 0부터 시작하니 잘 계산해야한다. iRow = 2; //미리 어느정도 그려진 엑셀이라면 GET으로 받아온다. row = sheet.getRow(iRow++); // 새로운 데이터를 넣어야 한다면 CREATE로 생성 row = sheet.createRow(iRow++); // 해당줄에 시작한 셀의 시작값 0붕터 시작. cell = row.getCell(0); // 해당 셀의 스타일 지정 cell.setCellType(cell.CELL_TYPE_STRING); // 필요 VALUE 지정 cell.setCellValue("넣을값"); //해당셀의 스타일 지정 cell.setCellStyle(cellStyle); //병합(시작 row, 병합 끝 row, 시작col, 끝 col) sheet.addMergedRegion(new CellRangeAddress(iRow, iRow+2, iCol, iCol+3 ) ); | cs |
1. 셀에 data를 넣을 row 지정
2. style 및 css 지정
3. 병합.. 그런데 병합부분은 저렇게 넣으면 css가 안먹히니 미리 병합할 셀들의 css를 주어야한다.
value는 첫번째로 들어옴.
ex)
1 2 3 4 5 6 7 8 9 10 11 12 | cell.create(0); cell.setCellStyle(cellStyle); cell.create(1); cell.setCellStyle(cellStyle); cell.create(2); cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(iRow, iRow+2, iCol, iCol+3 ) ); | cs |
필요 데이터를 다 생성하였다면 excel download 파일을 만들어야한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | fis.close(); file_nm = "템플릿 파일명.xlsx"; File outputFile = temporaryFileService.getNewFile(".xlsx"); OutputStream output = new FileOutputStream(outputFile); book.write(output); contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; if (outputFile.exists()) { exportStream = temporaryFileService.getFileAsStream(outputFile.getName()); file_nm = URLEncoder.encode(file_nm, "UTF-8").replace("+", "%20").replace("%28","(").replace("%29",")"); file_size = outputFile.length(); } Cookie cook = new Cookie(name, URLEncoder.encode(value, "utf-8")); cook.setPath("/"); response.addCookie(cook); | cs |
fis.close: input data를 닫아줌??
1. 템플릿 파일명 지정
2. 템플릿 파일 확장자 지정
3. content Type = Excel 파일로 type 변환
if 문 : 파일명이 공란이면 + 글자가 나오고 "()" 괄호관련 특수문자가 들어가면 %28, %29로 나와서 repalce 처리.
4. cooke 생성
*기존에 구현된 excel을 수정해서 한거라 문법및 여러부분이 틀릴수도있으니.. 틀린부분은 댓글로 알려주세요.!*
'JAVA' 카테고리의 다른 글
[JAVA] list 쿼리를 map으로 변환하여 Jsp에서 Jstl문으로 적용하기. (0) | 2018.11.20 |
---|---|
[JAVA] String 소수점 처리하기. (0) | 2018.05.14 |
Java 환경변수 설정 (0) | 2017.08.09 |