2017-11-28 5 views
0

일부 데이터가있는 Excel 시트를 만드는 프로그램을 만들었습니다. 열 1 - 문자열 포함 열 2- 숫자를 포함합니다 (기본적으로 정수이지만 ?이 플로트로 변환하기 이유 읽는 동안)Apache POI를 사용하여 정수로 숫자 셀 값을 검색하는 방법

public class ApachePOI8 { 

     public static void main(String[] args) { 

      String file = "C:\\Eclipse workspace\\PoiExample\\Excel8.xlsx"; 

      Workbook wb = new XSSFWorkbook();  
      Sheet sheet = wb.createSheet("Sheet1"); 
      sheet.protectSheet("Pwd1"); //This statements locks the entire sheet with password: Pwd1 

      DataFormat fmt = wb.createDataFormat(); //created a textStyle, which prevents Excel to interprate data, in short it means it will be taken as String 
      CellStyle textStyle = wb.createCellStyle(); 
      textStyle.setDataFormat(fmt.getFormat("@")); 
      textStyle.setLocked(false); 

      CellStyle numerStyle = wb.createCellStyle(); 
      numerStyle.setDataFormat(fmt.getFormat("0")); 
      numerStyle.setLocked(false); 


        Row row0 = CellUtil.getRow(0, sheet); 
        for (int columnIndex = 0; columnIndex<4; columnIndex++){ 
         Cell cell = CellUtil.getCell(row0, columnIndex); 
          if(columnIndex == 0) { 
           cell.setCellStyle(textStyle); 
           cell.setCellValue("Product 1"); 
          }else if(columnIndex == 1) {        
           cell.setCellStyle(numerStyle); 
           cell.setCellValue(1); 

          } 
         } 
          else{ 
           cell.setCellStyle(textStyle); 

          }  
        } //end of for Loop 


      try { 
       FileOutputStream outputStream = null; 
       outputStream = new FileOutputStream(file); 
       wb.write(outputStream); 
       outputStream.close(); 
       System.out.println("Done!"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

} 

이제

public class ApachePOIExcelRead { 

    private static final String FILE_NAME = "C:\\Eclipse workspace\\PoiExample\\Excel8.xlsx"; 
    public static void main(String[] args) { 

     try { 
      FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); 
      Workbook workbook = new XSSFWorkbook(excelFile); 
      Sheet datatypeSheet = workbook.getSheetAt(0); 
      Iterator<Row> iterator = datatypeSheet.iterator(); 

      while (iterator.hasNext()) { 

       Row currentRow = iterator.next(); 
       Iterator<Cell> cellIterator = currentRow.iterator(); 

       while (cellIterator.hasNext()) { 

        Cell currentCell = cellIterator.next(); 
        //getCellTypeEnum shown as deprecated for version 3.15 
        //getCellTypeEnum ill be renamed to getCellType starting from version 4.0 
        if (currentCell.getCellTypeEnum() == CellType.STRING) { 
         System.out.print("String: "+currentCell.getStringCellValue() + "\n"); 
        } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) { 
         System.out.print("NUMERIC "+currentCell.getNumericCellValue() + "\n"); 
        } 

       } 
       System.out.println(); 

      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

생성되었다 엑셀 시트를 읽어하지만 문자열로 출력을 얻고있다 : 제품 1을숫자 1.0 문자열 : 12-12-12

1 즉 입력 된 번호가 부동 오고 있음을 숫자 1.0 그래서?

친절히 제안합니다.

답변

0

해결책을 찾았을 때이 질문에 답하십시오. 다른 사람 :

DataFormat format = sheet1.getWorkbook().createDataFormat(); 
      CellStyle costprice = bulkUploadSpreadSheet.getWorkbook().createCellStyle(); 
      costprice.setDataFormat(format.getFormat("#")); 

에 도움이 이제 원하는 셀에 costprice 셀 스타일을 적용 할 수 있습니다. 다른 해결책이 있다면 알려주십시오. 모두 제일 좋다.

2

getNumericCellValue()double을 반환합니다.

POI에 관한 한 모든 수치는 부동 소수점입니다. 반환 된 doubleint 또는 long으로 변환 할 수 있습니다.이 과정에서 자르면 정수 여야합니다. 정수 결과를 "해야한다"고

System.out.print("NUMERIC " + ((int)currentCell.getNumericCellValue()) + "\n"); 

그러나 가끔 피연산자가 아닌 정수 식 세포의 숫자 값에 대한 놀라운 결과를 얻을 수 있음을 유의해야합니다. 실제 결과가 정확히 정수가 아니므로 자르면 끝나 버릴 수 있습니다. 모든 불량 정보를 읽으려면 What Every Computer Scientist Should Know About Floating-Point Arithmetic

+0

감사합니다. Jim에게 귀중한 조언을드립니다. :) –