Reputation: 1
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
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