Excel
Excel

Reputation: 1

Type mismatch: cannot convert from CellType to int

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

Upvotes: 0

Views: 4304

Answers (1)

Ondra K.
Ondra K.

Reputation: 3097

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.

Upvotes: 1

Related Questions