0

I am developing an automation testing code with the following error:

Type mismatch: cannot convert from CellType to int.

Please what can I do?

public static String cellToString(HSSFCell cell) {
        // TODO Auto-generated method stub

        int type;
        Object result;
        type = cell.getCellType();

        switch (type) {

        case 0 : // numeric value in Excel
            result = cell.getNumericCellValue();
            break;
        case 1 : //String value in Excel
            result = cell.getStringCellValue();
            break;
            default:
                throw new RuntimeException("No support for this of cell");
        }
        return result.toString();
}
ruohola
  • 21,987
  • 6
  • 62
  • 97
Excel
  • 1
  • 1
  • 1

1 Answers1

1

CellType is an enum, not an int. The type of your type variable should be CellType and your switch should look like this:

CellType type = cell.getCellType();
switch (type) {
    case CellType.NUMERIC : // numeric value in Excel
        result = cell.getNumericCellValue();
        break;
    case CellType.STRING : //String value in Excel
        result = cell.getStringCellValue();
        break;
        default:
            throw new RuntimeException("No support for this of cell");
    }

Alternatively, you can use Enum#ordinal(), which returns an ordinal integer of the enum value, but the example above is much preferable.

EDIT: Also have a look at this answer about how to get cell value as string using Formatter instead of switch.

Ondra K.
  • 2,767
  • 4
  • 23
  • 39