工具下载:https://download.youkuaiyun.com/download/pnc_logon/10870766
支持公式需要FormulaEvaluator 中evaluate方法,该方法说明如下:
/**
* If cell contains a formula, the formula is evaluated and returned,
* else the CellValue simply copies the appropriate cell value from
* the cell and also its cell type. This method should be preferred over
* evaluateInCell() when the call should not modify the contents of the
* original cell.
* @param cell
*/
CellValue evaluate(Cell cell);
如果是日期,需要先判断是否是NUMERIC类型,否则会抛异常
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
public static String getValue(FormulaEvaluator evaluator, Cell cell) {
CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return "";
}
CellType type = cellValue.getCellTypeEnum();
switch (type) {
case _NONE:
case BLANK:
return "";
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
// 用于转化为日期格式
Date d = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm");
String timeStr = formater.format(d);
return timeStr;
}
// poi对数字统一处理为double类型,会让填写的1变为1.0,此处做手动处理
double doubleVal = cell.getNumericCellValue();
long longVal = Math.round(cell.getNumericCellValue());
if (Double.parseDouble(longVal + ".0") == doubleVal) {
return String.valueOf((int)doubleVal);
} else {
return String.valueOf(doubleVal);
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return String.valueOf(cell.getStringCellValue());
}
}
其中wb = XSSFWorkbook(instream);