/** * 讀具體數值 * * @param cell * @return *///處理公式中讀取出來的數字精度問題
private static NumberFormat numberFormat = NumberFormat.getInstance();idestatic { numberFormat.setGroupingUsed(false); } public static String getCellValue(Cell cell) { String value = null; if (cell != null) { // 把數字當成String來讀,避免出現1讀成1.0的狀況 if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { cell.setCellType(Cell.CELL_TYPE_STRING); } switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA: // cell.getCellFormula(); try { //處理公式中讀取出來的數字精度問題 value = numberFormat.format(cell.getNumericCellValue()); // value = String.valueOf(cell.getNumericCellValue()); } catch (IllegalStateException e) { value = String.valueOf(cell.getRichStringCellValue()); } break; case Cell.CELL_TYPE_NUMERIC: value = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: value = String.valueOf(cell.getRichStringCellValue()); break; } } return value; }