Alternative to deprecated getCellType

JavaApache Poi

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

Use getCellType()

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

Solution 4 - Java

    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.

Solution 5 - Java

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.

Solution 6 - Java

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...

Solution 7 - Java

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

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1766169View Question on Stackoverflow
Solution 1 - JavaTomasz StanczakView Answer on Stackoverflow
Solution 2 - Javauser7171758View Answer on Stackoverflow
Solution 3 - JavaBenigno SalesView Answer on Stackoverflow
Solution 4 - JavaArindamView Answer on Stackoverflow
Solution 5 - JavaDimaSanView Answer on Stackoverflow
Solution 6 - JavafrvaView Answer on Stackoverflow
Solution 7 - JavaSrinivasView Answer on Stackoverflow
Solution 8 - JavashanView Answer on Stackoverflow