javaexcelapache

Deprecated CellType method


I've seen other forums here with suggestions for solutions, I don't get any of them to work.

I want to check if a cell is either null or blank, the deprecated code that I use is (both getCellType() and CELL_TYPE_BLANK is deprecated):

if( (c == null) || c.getCellType() == c.CELL_TYPE_BLANK){
//do something
}

For example, I've been looking at the solution in this thread:

Alternative to deprecated getCellType

and I was thinking that a solution could possibly look like this:

if( (c == null) || c.getCellTypeEnum() == CellType.BLANK){
//Error: incomparable types: org.apache.poi.ss.usermodel.CellType and int
//do something
}

or

if( (c == null) || c.getBooleanCellValue()){
//do something
}

But it doesn't work, and apaches documentation is not that helpful either. Does anyone have a solution that doesn't produce warnings? I'm using poi 3.17.


Solution

  • I've been struggling with the same issue and I found the following works for me.

    It is the inverse of your approach. I check if the value is not null.

    The code below is for handling string values:

    private static String checkForNullString(Cell cellToCheck) {
        String strCheck;
        if (cellToCheck != null && cellToCheck.getCellTypeEnum() != CellType.BLANK) {
            cellToCheck.setCellType(CellType.STRING);
            strCheck = cellToCheck.getStringCellValue();
        }
        else
            strCheck = "";
        return strCheck;
    }
    

    To handle numeric values, simply change the following:

    cellToCheck.getStringCellValue();
    

    to

    cellToCheck.getNumericCellValue());