2011-01-04 4 views
5

나는 시트의 내용을 검색 할 수 주어진 예제 코드를 사용하고 :Apache POI를 사용하여 특정 날짜의 Excel 시트를 검색하는 방법은 무엇입니까?

Sheet sheet1 = wb.getSheetAt(0); 
for (Row row : sheet1) { 
for (Cell cell : row) { 
    CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); 
    System.out.print(cellRef.formatAsString()); 
    System.out.print(" - "); 

    switch(cell.getCellType()) { 
     case Cell.CELL_TYPE_STRING: 
     System.out.println(cell.getRichStringCellValue().getString()); 
     break; 
     case Cell.CELL_TYPE_NUMERIC: 
     if(DateUtil.isCellDateFormatted(cell)) { 
      System.out.println(cell.getDateCellValue()); 
     } else { 
      System.out.println(cell.getNumericCellValue()); 
     } 
     break; 
     case Cell.CELL_TYPE_BOOLEAN: 
     System.out.println(cell.getBooleanCellValue()); 
     break; 
     case Cell.CELL_TYPE_FORMULA: 
     System.out.println(cell.getCellFormula()); 
     break; 
     default: 
     System.out.println(); 
    } 
} 

}

그러나 제대로 지정된 날짜와 시트로부터 추출 된 날짜를 비교하는 방법을 찾을 수 없습니다. 데이터 유효성 검사를 사용하고 날짜를 문자열로 포맷하고 추가 조건문을 포함하여 시도했지만 아무것도 작동하지 않았습니다.

은 비교적 간단한 문제인 것처럼 보이지만 아직 해결할 수 없었습니다.

어떤 도움을 주시면 대단히 감사하겠습니다!

답변

3

Excel에서는 날짜가 부동 소수점 소수점으로 저장됩니다. 이는 분명히 Java가 날짜를 처리하는 방식과 다릅니다 (long 값).

Apache POI에는 Java와 Excel 간의 날짜 변환을 수행하는 도우미 클래스가 포함되어 있습니다 (DateUtil). 이것은 도움이 될 것입니다 (문제가 해결 된 경우).

+0

가 DateUtil 놀러 관리 및 문자열과 getExcelDate으로 변환 솔루션의 일종을 가지고, 도움을 주셔서 감사합니다 – Jsk

0

시트에서 숫자를 읽으면 문자열로 읽더라도 10 진수로 표시됩니다.

나는 셀의 모든 데이터를 밑줄 '_'로 시작하므로 모든 셀의 데이터를 문자열로 읽으려는 경우 십진수 또는 아무 것도 얻지 못합니다.

String cellValue = cell.getValueAsString(); // assuming the cell value is "_01/01/2011" 
cellValue = cellValue.substring(1);// this will ignore first character, the '_' 
0

사용 DataFormatter 클래스 (아파치 POI) :

DataFormatter df = new DataFormatter(); 
String cellValue = df.formatCellValue(Cell cell);