天天看點

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);
	}