1. 程式人生 > >poi匯入資料格式 小數點處理

poi匯入資料格式 小數點處理

使用poi匯入excel時,不論是不設定成文字格式,資料依舊會轉成資料格式,讀取的時候會帶上小數點,所以轉換的時候加入條件去掉小數點

				case Cell.CELL_TYPE_NUMERIC:
					// 此處不需要格式化資料,否則excel中的小數在此會變成四捨五入後的整數
					/*
					 * DecimalFormat df = new DecimalFormat("0"); val =  df.format(cell.getNumericCellValue());
					 */
					if (DateUtil.isCellDateFormatted(cell)) {
						val = String.valueOf(cell.getDateCellValue());
					} else {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						String temp = cell.getStringCellValue();
						// 判斷是否包含小數點,如果不含小數點,則以字串讀取,如果含小數點,則轉換為Double型別的字串
						if (temp.indexOf(".") > -1) {
							val = String.valueOf(new Double(temp)).trim();
						} else {
							val = temp.trim();
						}
					}