现在用POI处理Excel文件,现在某一行中,其某个单元格中的现有值是数值型的值,但是我们应当把其做字符串处理的,但是现在读该单元格,我只能用getNumericCellValue()方法来读取,而如果用getStringCellValue()方法读的话,就会出现“java.lang.NumberFormatException: You cannot get a string value from a numeric cell”的错误。我用setCellType(1),想将单元格的值的类型转为String型,但是也是无法成功,请教各位有什么好办法,多谢了。
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(12345);
这样你就可以使用
cell.getStringCellValue();来读
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:strCell = String.valueOf(cell.getBooleanCellValue());
break;
default:strCell = "";
break;
}