[java] Alternative to deprecated getCellType

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

This is my code:

try (FileInputStream fileInputStream = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(file)) {
    XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "(Integer)\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "(String)\t");
                    break;
            }
        }
        System.out.println("");
    }
} catch (Exception e) {
    e.printStackTrace();
}

I get a warning that cell.getCellType() is deprecated. Can anyone tell me the alternative?

This question is related to java apache-poi

The answer is


Use getCellType()

switch (cell.getCellType()) {
   case BOOLEAN :
                 //To-do
                 break;
   case NUMERIC:
                 //To-do
                 break;
   case STRING:
                 //To-do
                 break;
}

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.


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;
}

It looks that 3.15 offers no satisfying solution: either one uses the old style with Cell.CELL_TYPE_*, or we use the method getCellTypeEnum() which is marked as deprecated. A lot of disturbances for little add value...


    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.


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


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();
}