在我的代码中,我正在检查单元格类型,如果单元格类型是公式,那么我将尝试获取缓存的值,如下所示:
else if(cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA){
System.out.println("formula result type:" +cell.getCachedFormulaResultType());
switch(cell.getCachedFormulaResultType()){
case XSSFCell.CELL_TYPE_NUMERIC:
....
....
break;
它对于某些单元格工作正常,但在其中一个单元格中,我得到非法状态异常:
java.lang.IllegalStateException: Only formula cells have cached results
我无法找出错误的原因。
我没有在此 else if
之外使用 cell.getCachedFormulaResult()
,甚至这也适用于前面的所有单元格(具有相同的公式)
最佳答案
如果缓存的结果不可用,也许有助于显式触发计算:
Workbook wb = ...
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
// suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
CellValue cellValue = evaluator.evaluate(cell);
你可以做这样的事情,例如在 try/catch(IllegalStateException)
关于java - POI-XSSF : cell type is FORMULA, 但无法获取cachedResultType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199314/