天天看点

java使用poi解析或处理excel的时候,防止数字变成科学计数法的方法

现在还在加班,趁有些时间就把刚刚操作excel表中遇到的一个坑的解决方法给写出了了。这个坑如题,解决方法就是使用
<pre name="code" class="java">DecimalFormat df = new DecimalFormat("0");  
val = df.format(cell.getNumericCellValue());  
           

这个方法。

/**
	 * 普通类型值设置
	 * 
	 * @param rows
	 * @param obj
	 * @param cellConfig
	 * @throws Exception
	 * @throws CoreException
	 */
	private static void setNormalVal(List<Row> rows, Object obj, CellConfig cellConfig)
			throws Exception, CoreException {
		Cell cell = rows.get(0).getCell(cellConfig.getIndex());
		String val = "";
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_STRING:
			val = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_BOOLEAN:
			Boolean val1 = cell.getBooleanCellValue();
			val = val1.toString();
			break;
		case Cell.CELL_TYPE_NUMERIC:
			DecimalFormat df = new DecimalFormat("0");  
			val = df.format(cell.getNumericCellValue());  
			break;
		case Cell.CELL_TYPE_BLANK:
			break;
		default:
			throw new CoreException("数据类型配置不正确");
		}
		ReflectUtil.setObjField(obj, cellConfig.getValName(), String.class, val);
	}