Skip to main content
 首页 » 编程设计

poi读取excel单元格的数字格式化

2022年08月02日140xiaohuochai

我的需求是,在excel显示的啥导入进来就是啥

但实际poi导入后,数字经常会转换为科学计数法,或者是莫名多了很多位小数


	public static Object getCellValue(org.apache.poi.ss.usermodel.Cell hssfCell) { 
		      if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { 
		          // 返回布尔类型的值 
		          return String.valueOf(hssfCell.getBooleanCellValue()); 
		      } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { 
		          // 返回数值类型的值 
		          Object inputValue = null;// 单元格值 
		          Long longVal = Math.round(hssfCell.getNumericCellValue()); 
		          Double doubleVal = hssfCell.getNumericCellValue(); 
		          if(Double.parseDouble(longVal + ".0") == doubleVal){   //判断是否含有小数位.0 
		              inputValue = longVal; 
		          } 
		          else{ 
		              inputValue = doubleVal; 
		          } 
		          DecimalFormat df = new DecimalFormat("#.##########");  //格式化为n位小数,按需修改 
		          return String.valueOf(df.format(inputValue));      //返回String类型 
		      } else { 
		          // 返回字符串类型的值 
		          return String.valueOf(hssfCell.getStringCellValue()); 
		      } 
	}



本文参考链接:https://yfx000.blog.csdn.net/article/details/79583420