来自:http://blog.csdn.net/cuihaiyang/article/details/7193027
private static String getStringCellValue(Cell cell) {// 获取单元格数据内容为字符串类型的数据
String strCell = "";
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
// cell.getCellFormula();
try {
/*
* 此处判断使用公式生成的字符串有问题,因为HSSFDateUtil.isCellDateFormatted(cell)判断过程中cell
* .getNumericCellValue();方法会抛出java.lang.NumberFormatException异常
*/
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
strCell = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) +"-" + date.getDate();
break;
} else {
strCell = String.valueOf(cell.getNumericCellValue());
}
} catch (IllegalStateException e) {
strCell = String.valueOf(cell.getRichStringCellValue());
}
break;
case Cell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
strCell = getDateCellValue(cell);
break;
} else {
strCell = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
return strCell;
}
这个方法在导入没有公式的Excel时没有问题,今天遇到含有公式的Excel,如图:
报错信息如下:
经调试发现HSSFDateUtil.isCellDateFormatted(cell)判断过程中会执行cell.getNumericCellValue();方法,字符串“201201FSSH300466”在转换成数字的时候出错了,在网上搜了一下,大多数的解决方法是:
[java] view plaincopy
case HSSFCell.CELL_TYPE_FORMULA:
try {
value = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
value = String.valueOf(cell.getRichStringCellValue());
}
break;
经测试发现,在执行cell.getNumericCellValue()方法时还是抛出NumberFormatException异常,于是将try/catch中的方法互换:
[java] view plaincopy
case HSSFCell.CELL_TYPE_FORMULA:
try {
strCell = String.valueOf(cell.getStringCellValue());
} catch (IllegalStateException e) {
strCell = String.valueOf(cell.getNumericCellValue());
}
break;
这样对于字符串cell.getStringCellValue()方法即可取得其值,如果公式生成的是数值,使用cell.getStringCellValue()方法会抛出IllegalStateException异常,在异常处理中使用cell.getNumericCellValue();即可。
分享到:
相关推荐
poi excel通过模板导出excel表格 且带有公式 导出的excel表格里面的公式均可计算 有简单的公式也有难的公司 在此例子里面都可以跟着excel一起到处并可计算
POI中设置Excel单元格格式
java excel poi合并单元格
excel poi工具类 导入 导出 合并单元格 计算公式
POI excel单元格顔色设定
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
利用poi进行excel相同内容合并单元格,只摘要了传入部分功能,请关注addMergedRegion方法
使用Poi读取大数据量excel的方法 支持2003和2007的版本
poi获取exccel有合并单元格的数据,存入List
POI生成Excel POI读取Excel java操作Excel Servlet生成Excel web项目,包含含读取Excel与生成Excel方法
该工具类是poi基于事件驱动去读取海量excel数据,解决内存溢出以及空单元格的问题。 该压缩包里面有两个文件,TestExcel.java主要是原始的处理方法,操作简单,适合小数据量的读取。 ExampleEventUserModelUtil.java...
poi excel poi excel poi excel poi excel poi excel poi excel poi excel poi excel
poi获取合并单元格,Java 对excel 文档处理。
java使用poi在excel单元格添加超链接,设置字体颜色(csdn)————程序
poi导入/导出Excel表格,合并单元格的读取和设置
poi生成excel及合并单元格示例,详见文章http://blog.csdn.net/geloin/article/details/17219885
使用poi读取写入复杂excel内容包括样式,工具类
Java操纵POI解析Excel表,根据表头将内容储存。通过实体类set、get方便其它方法引用
poi读取合并单元格帮助类
poi读取大数据量excel文件,避免内存溢出,行级操作 根据本网站的资源修改的。 将一些类路径错误全部进行了修正。 另外,需要自己在类路径里,放spring-context.jar和spring-beans.jar包。