如何使用apache poi 3.1获取公式单元格值(数据)

时间:2014-04-10 15:44:11

标签: java excel apache excel-formula apache-poi

我在我的应用程序中使用Apache poi-3.1-FINAL-20080629。在这里,我有一个使用公式的问题...

My Cell有公式(sheet2!C10),此单元格内的数据是字符串类型(例如$ 3,456)...如何访问该单元格也想显示公式。

我的代码看起来像:

HSSFWorkbook wb = new HSSFWorkbook(file);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);

Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
        HSSFRow row = (HSSFRow) rows.next();
        System.out.println("\n");
        Iterator cells = row.cellIterator();
        while (cells.hasNext()) {

            HSSFCell cell = (HSSFCell) cells.next();

            int cellType = cell.getCellType();

            if (HSSFCell.CELL_TYPE_NUMERIC == cellType)
                System.out.print(cell.getNumericCellValue() + "     ");
            else if (HSSFCell.CELL_TYPE_STRING == cellType)
                System.out.print(cell.getStringCellValue() + "     ");
            else if (HSSFCell.CELL_TYPE_BOOLEAN == cellType)
                System.out.print(cell.getBooleanCellValue() + "     ");
            else if (HSSFCell.CELL_TYPE_BLANK == cellType)
                System.out.print("BLANK     ");
            else if (HSSFCell.CELL_TYPE_FORMULA == cellType) {
                System.out.print(evaluator.evaluateInCell(cell).toString()  );              }   else 
                System.out.print("Unknown cell type " + cellType);

        }

    }

evaluateator.evaluateInCell(cell).toString()抛出空指针异常。请帮助!!!

2 个答案:

答案 0 :(得分:13)

好的,首先,Apache POI 3.1相当陈旧。线索在罐子名称 - poi-3.1-FINAL-20080629的日期是2008年,所以6年前!值得升级的是number of bug fixes since then is pretty vast....

至于你的问题,我相当确定你不想打电话给evaluator.evaluateInCell。这几乎从来都不是正确的方法来调用

您有三种选择,具体取决于您对公式单元格的处理方式:

我想要公式字符串

致电Cell.getCellFormula,您将获得公式字符串

我想要计算最后一个公式值Excel

当Excel写出一个文件时,它通常会将公式的最后一个评估值存储在缓存中,以使开放变得美观和快速。您可以从Apache POI中读取此信息(如果存在)。它只是一个缓存,所以它可能并不总是正确的,但它通常是。

您需要拨打Cell.getCachedFormulaResultType,然后启用它,并使用普通的getter读取值,例如

int cellType = cell.getCellType();
handleCell(cell, cellType);

private void handleCell(Cell cell, int cellType) {
        if (HSSFCell.CELL_TYPE_NUMERIC == cellType)
            System.out.print(cell.getNumericCellValue() + "     ");
        else if (HSSFCell.CELL_TYPE_STRING == cellType)
            System.out.print(cell.getStringCellValue() + "     ");
        else if (HSSFCell.CELL_TYPE_BOOLEAN == cellType)
            System.out.print(cell.getBooleanCellValue() + "     ");
        else if (HSSFCell.CELL_TYPE_BLANK == cellType)
            System.out.print("BLANK     ");
        else if (HSSFCell.CELL_TYPE_FORMULA == cellType)
            handleCell(cell, cell.getCachedFormulaResultType());
        else 
            System.out.print("Unknown cell type " + cellType);
   }

我希望Apache POI为我计算公式结果

这里最好的选择是获得FormulaEvaluator object并致电评估:

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);

switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
    System.out.println(cellValue.getBooleanValue());
    break;
case Cell.CELL_TYPE_NUMERIC:
    System.out.println(cellValue.getNumberValue());
    break;
case Cell.CELL_TYPE_STRING:
    System.out.println(cellValue.getStringValue());
    break;
case Cell.CELL_TYPE_BLANK:
    break;
case Cell.CELL_TYPE_ERROR:
    break;

// CELL_TYPE_FORMULA will never happen
case Cell.CELL_TYPE_FORMULA: 
    break;
}   

Apache POI Formula Evaluation page在所有这些

上有更多

答案 1 :(得分:0)

最好这样做。

for(Cell cell : row) {
     if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        System.out.println("Formula is " + cell.getCellFormula());
        switch(cell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.println("Last evaluated as: " + cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
                break;
        }
     }
 } 

之前我使用FormulaEvaluator的解决方案,在这种情况下,我遇到了一些例外,如下所示。

org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell Sheet1!BL190
    at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:332)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:273)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:205)
    at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateFormulaCellValue(XSSFFormulaEvaluator.java:268)
    at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluate(XSSFFormulaEvaluator.java:121)