0

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 depriciated code that I use is (both getCellType() and CELL_TYPE_BLANK is depreciated):

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

For example I've been looking at 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 does'nt work and apaches documentation is not that helpful either. Does anyone have a solution that does'nt produce warnings? I'm using poi 3.17.

BR

1 Answers1

0

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());
Abdul G
  • 36
  • 1
  • 1
  • 5