Alternative to deprecated getCellType

后端 未结 8 2252
自闭症患者
自闭症患者 2020-11-27 05:44

I\'m reading an excel-file (file extension xlsx) using org.apache.poi 3.15.

This is my code:

try (FileInputStream fileInputStream = new FileInputStr         


        
相关标签:
8条回答
  • 2020-11-27 05:53

    You can use:

    cell.getCellTypeEnum()
    

    Further to compare the cell type, you have to use CellType as follows:-

    if(cell.getCellTypeEnum() == CellType.STRING){
          .
          .
          .
    }
    

    You can Refer to the documentation. Its pretty helpful:-

    https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

    0 讨论(0)
  • 2020-11-27 06:02
        FileInputStream fis = new FileInputStream(new File("C:/Test.xlsx"));
    
        //create workbook instance
        XSSFWorkbook wb = new XSSFWorkbook(fis);
    
        //create a sheet object to retrieve the sheet
        XSSFSheet sheet = wb.getSheetAt(0);
    
        //to evaluate cell type
        FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
    
        for(Row row : sheet)
        {
            for(Cell cell : row)
            {
                switch(formulaEvaluator.evaluateInCell(cell).getCellTypeEnum())
                {
                case NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    break;
                case STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    break;
                default:
                    break;
    
                }
            }
            System.out.println();
        }
    

    This code will work fine. Use getCellTypeEnum() and to compare use just NUMERIC or STRING.

    0 讨论(0)
  • 2020-11-27 06:06

    You can do this:

    private String cellToString(HSSFCell cell) {
        CellType type;
        Object result;
        type = cell.getCellType();
    
        switch (type) {
            case NUMERIC :  //numeric value in excel
                result = cell.getNumericCellValue();
                break;
            case STRING : //String Value in Excel
                result = cell.getStringCellValue();
                break;
            default :
                throw new RuntimeException("There is no support for this type of value in Apche POI");
        }
        return result.toString();
    }
    
    0 讨论(0)
  • 2020-11-27 06:07

    For POI 3.17 this worked for me

    switch (cellh.getCellTypeEnum()) {
        case FORMULA: 
            if (cellh.getCellFormula().indexOf("LINEST") >= 0) {
                value = Double.toString(cellh.getNumericCellValue());
            } else {
                value = XLS_getDataFromCellValue(evaluator.evaluate(cellh));
            }
            break;
        case NUMERIC:
            value = Double.toString(cellh.getNumericCellValue());
            break;
        case STRING:
            value = cellh.getStringCellValue();
            break;
        case BOOLEAN:
            if(cellh.getBooleanCellValue()){
                value = "true";
            } else {
                value = "false";
            }
            break;
        default:
            value = "";
            break;
    }
    
    0 讨论(0)
  • 2020-11-27 06:08

    The accepted answer shows the reason for the deprecation but misses to name the alternative:

    CellType    getCellTypeEnum()
    

    where the CellType is the enum decribing the type of the cell.

    The plan is to rename getCellTypeEnum() back to getCellType() in POI 4.0.

    0 讨论(0)
  • 2020-11-27 06:16

    From the documentation:

    int getCellType() Deprecated. POI 3.15. Will return a CellType enum in the future.

    Return the cell type. Will return CellType in version 4.0 of POI. For forwards compatibility, do not hard-code cell type literals in your code.

    0 讨论(0)
提交回复
热议问题