POI-根据Cell获取对应的String类型值
2017-03-27 00:07
483 查看
目的
由于工作中对读取出来的cellValue统一以字符串类型存储,故有此方法。方案
懒得打字,看图如下:代码
主方法(根据不同类型采取不同策略)
/** * 根据不同情况获取Java类型值 * <ul><li>空白类型<ul><li>返回空字符串</li></ul></li></ul><ul><li>布尔类型</li><ul><li>返回Boulean类型值</li></ul></ul><ul><li>错误类型</li><ul><li>返回String类型值:Bad value</li></ul></ul><ul><li>数字类型</li><ul><li>日期类型</li><ul><li>返回格式化后的String类型,e.g.2017-03-15 22:22:22</li></ul><li>数字类型</li><ul><li>返回经过处理的java中的数字字符串,e.g.1.23E3==>1230</li></ul></ul> </ul><ul><li>公式类型</li><ul><li>公式正常</li><ul><li>返回计算后的String类型结果</li></ul></ul><ul><li>公式异常</li><ul><li>返回错误码,e.g.#DIV/0!;#NAME?;#VALUE!</li></ul></ul> </ul><ul><li>字符串类型</li><ul><li>返回String类型值</li></ul></ul> * * @param cell * XSSFCell类型单元格 * @return 返回Object类型值 * @since 2017-03-26 00:05:36{@link #getValueOfNumericCell()} */ public static Object getJavaValue(XSSFCell cell) { Object o = null; int cellType = cell.getCellType(); switch (cellType) { case XSSFCell.CELL_TYPE_BLANK: o = ""; break; case XSSFCell.CELL_TYPE_BOOLEAN: o = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_ERROR: o = "Bad value!"; break; case XSSFCell.CELL_TYPE_NUMERIC: o = getValueOfNumericCell(cell); break; case XSSFCell.CELL_TYPE_FORMULA: try { o = getValueOfNumericCell(cell); } catch (IllegalStateException e) { try { o = cell.getRichStringCellValue().toString(); } catch (IllegalStateException e2) { o = cell.getErrorCellString(); } } catch (Exception e) { e.printStackTrace(); } break; default: o = cell.getRichStringCellValue().getString(); } return o; }
获取数字类型Cell的字符串
// 获取数字类型的cell值 private static Object getValueOfNumericCell(XSSFCell cell) { Boolean isDate = DateUtil.isCellDateFormatted(cell); Double d = cell.getNumericCellValue(); Object o = null; if (isDate) { o = DateFormat.getDateTimeInstance() .format(cell.getDateCellValue()); } else { o = getRealStringValueOfDouble(d); } return o; }
处理科学计数法带来的麻烦
// 处理科学计数法与普通计数法的字符串显示,尽最大努力保持精度 private static String getRealStringValueOfDouble(Double d) { String doubleStr = d.toString(); boolean b = doubleStr.contains("E"); int indexOfPoint = doubleStr.indexOf('.'); if (b) { int indexOfE = doubleStr.indexOf('E'); // 小数部分 BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint + BigInteger.ONE.intValue(), indexOfE)); // 指数 int pow = Integer.valueOf(doubleStr.substring(indexOfE + BigInteger.ONE.intValue())); int xsLen = xs.toByteArray().length; int scale = xsLen - pow > 0 ? xsLen - pow : 0; doubleStr = String.format("%." + scale + "f", d); } else { java.util.regex.Pattern p = Pattern.compile(".0$"); java.util.regex.Matcher m = p.matcher(doubleStr); if (m.find()) { doubleStr = doubleStr.replace(".0", ""); } } return doubleStr; }
相关文章推荐
- POI-根据Cell获取对应的String类型值
- 根据string获取对应类型的对应属性
- Android根据文件名(String类型)去查找R文件中的对应id(int类型)
- 根据字符串获取对应类型(Type) 转
- Go实战--golang中获取公网ip、查看内网ip、检测ip类型、校验ip区间、ip地址string和int转换、根据ip判断地区国家运营商等
- C#根据字符串获取对应类型(Type) 转
- java POI根据单元格的类型获取Excel单元格的内容
- Apache.POI.HSSF获取单元格数据为String类型(参考源码)新
- POI 中的CellType类型以及值的对应关系
- sql根据表名获取字段及对应说明类型长度
- iOS开发---根据button获取相对应的cell
- 新版POI如何获取日期类型的cell的值
- POI操作Excel:cell的背景颜色类型
- 将任意一个对象中的所有String类型 的成员变量所对应字符串中的“b”改成“a”(利用反射)
- flex内根据id(string)获取对象
- (java)获取某文件夹下面所有的指定类型的文件名称,并根据情况返回可用的名称
- Windows CE如何根据文件名获取其对应文件图标icon
- 根据FileUpload控件名获取上传文件(大小)类型
- response.setContentType()的String参数及对应类型
- Poi Cell类型与数值对照