2016-05-17 127 views
0

我想评估公式并从保持其值的单元格中移除公式。这怎么可以使用Apache POI完成?评估公式并从单元格中删除公式

下面是我的代码,我评估所有的公式。

 FormulaEvaluator evaluator = template.getCreationHelper().createFormulaEvaluator(); 
     int sheetCount = template.getNumberOfSheets(); 
     for(int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex ++) { 
      sheet = dfaTemplate.getSheetAt(sheetIndex); 
      for (Row r : sheet) { 
       for (Cell c : r) { 
        if (c.getCellType() == Cell.CELL_TYPE_FORMULA) { 
         evaluator.evaluateFormulaCell(c); 
        } 
       } 
      } 
     } 
+1

您是否试过'evaluateator.evaluateInCell(cell)'?请参阅https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/FormulaEvaluator.html#evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)。 – Priyesh

+0

@Priyesh:也许你应该把它写成答案而不是评论。 – jmarkmurphy

回答

1

您应该使用evaluator.evaluateInCell(cell)而不是evaluator.evaluateFormulaCell(c)。如果单元格包含公式,evaluateInCell将计算公式,并将公式结果放回到单元格中,而不是旧公式。

有关更多信息,请参阅evaluator.evaluateInCell(cell)