直接POI導入Excel中的數據的時候,直接獲取表中的值,如果表中單元格的值時由公式計算得出的話,獲取到的會是公式
所以需要對獲取的單元格的值進行處理:
/**
* 導入數字時。導入公式的計算結果而非公式
* @param cell
* @return
*/
public static String getCellValue(XSSFCell cell) {
CellType cellType = cell.getCellTypeEnum();
String cellValue = "";
switch (cellType) {
case NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case FORMULA:
try {
cellValue = cell.getStringCellValue();
} catch (IllegalStateException e) {
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
default:
cellValue = cell.getStringCellValue();
}
return cellValue.trim();
}