엄지월드

[Excel POI] 시간만 저장하기 본문

java

[Excel POI] 시간만 저장하기

킨글 2017. 9. 16. 12:12
반응형

엑셀에 시간만 넣고 싶은데 년도까지 같이 들어가는 현상이 있었다.

(1900-01-01 4:00:00 PM -> 4:00:00 PM 으로 바꾸고 싶었던 것이다)

그래서 doubleTime 값을 출력해보니 25569.541666666668, 25569.541666666668 이런식으로 나와서 

25569를 빼줬더니 원하는 대로 HH:mm 만나오게 되었다! 

 

// 초기 설정 부분 

XSSFSheet sheet = workbook.createSheet("Excel Sheet"); // 엑셀 시트를 생성한다.
XSSFRow row=null;
XSSFCell cell=null;
XSSFCellStyle csTimeFormat = workbook.createCellStyle(); // Cell을 생성한다.
csTimeFormat.setDataFormat(format.getFormat("hh:mm")); // 엑셀에서 날짜 형식을 지정한다.

//상,하,좌,우 중앙 정렬

csTimeFormat.setAlignment(HorizontalAlignment.CENTER);
csTimeFormat.setVerticalAlignment(VerticalAlignment.CENTER);
csTimeFormat.setBorderRight(CellStyle.BORDER_THIN); csTimeFormat.setBorderLeft(CellStyle.BORDER_THIN); csTimeFormat.setBorderTop(CellStyle.BORDER_THIN); csTimeFormat.setBorderBottom(CellStyle.BORDER_THIN);

// 실제로 엑셀 만들기

row = sheet.createRow((short)0);
cell = row.createCell(0);

// 데이터 처리

cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellStyle(csTimeFormat); 

double doubleTime =DateUtil.getExcelDate((Time)getColumn.get(j));
cell.setCellValue(doubleTime-25569); // 1900년01월01일을 빼준다.

System.out.println("doubleTime:"+doubleTime);

 

Comments