【POI解析XLS】得到Cell的数值为科学计数法和.0的解决办法

  用POI解析XLS时,用row.getCell((short)0).getNumericCellValue()的值,如果数值小一点,那么就会自动补0.,

如果数值很大,就会被自动转成科学计数法。

  针对自动补.0的情况,使用NumberFormat类的format方法,如下:

NumberFormat nf = NumberFormat.getInstance();
//这样就去掉了.0
String str = nf.format(row.getCell((short)0).getNumericCellValue());
/*format源码*/
public final String format(double number){
    String result = fastFormat(number);
    if(result!=null)
        return result;
    return format(number,new     
        StringBuffer(),DontCareFiledPosition.INSTANCE).toString();
}

  但是这样得到的str,虽然没有.0,但是却多了一个千位符的“,”,即100535.0被转成了100,535。所以还需要去掉逗号:

NumberFormat nf = NumberFormat.getInstance();
//这样就去掉了.0
String str = nf.format(row.getCell((short)0).getNumericCellValue());
//去掉逗号
str=str.replace(",","");

  这样得到的str就是我们想要的结果。

原文地址:https://www.cnblogs.com/zhuii/p/11736938.html