Apachie POI中的FormulaEvaluator仅返回空值

时间:2018-09-26 01:04:05

标签: java excel apache-poi

我正在尝试评估Excel文件中的公式,并获取以字符串形式发送回的值。单元格中的值显示为日期,但我不需要将其作为日期发送回去,我只想将其作为字符串。


下面是我的代码:

public final class Demo {

    public static void main(final String[] args) throws Exception {
        System.out.println(getCellValue());
}

    public static String getCellValue() throws Exception {
    final Workbook workBook = WorkbookFactory.create(new FileInputStream(new File("config.xlsx")));
        final Sheet sheet = workBook.getSheetAt(0);

        final Row row = sheet.getRow(1);
        final Cell cell = row.getCell(5);

        System.err.println(cell.getCellFormula());

        final FormulaEvaluator evaluator = workBook.getCreationHelper().createFormulaEvaluator();
        final CellValue cellValue = evaluator.evaluate(cell);
        return cellValue.getStringValue();
        //Answer discovered instead of return "cellValue.getStringValue();" you must use only "return cellValue;"
    }
}



当我运行代码时,控制台会向我显示出现在单元格中的公式,但对于该值,它仅返回null。知道我在做什么错吗?

TEXT(VLOOKUP((SUMIFS(A:A,B:B,"<="&G2,C:C,">="&G2)),A:C,2,0),"m/d/yyyy") null

编辑: 更新的代码

    FileInputStream fis = new FileInputStream("config.xlsx");
    Workbook wb = new XSSFWorkbook(fis);
    Sheet sheet = wb.getSheetAt(0);
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

    CellReference cellReference = new CellReference("F2"); 
    Row row = sheet.getRow(cellReference.getRow());
    Cell cell = row.getCell(cellReference.getCol()); 

    CellValue cellValue = evaluator.evaluate(cell);

            System.out.println(cellValue.getCellType());
        //Answer discovered instead of return "System.out.println(cellValue.getCellType());" you must use only "System.out.println(cellValue);"
    wb.close();

1 个答案:

答案 0 :(得分:1)

对不起,我无法复制您的问题。我已经下载了您的文件。然后在运行以下代码时:

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.File;

class ReadExcelFormulaEvaluatorExample {

 static Workbook workBook;
 static Sheet sheet;
 static FormulaEvaluator evaluator;
 static DataFormatter formatter;

 static {
  try {
   workBook = WorkbookFactory.create(new FileInputStream(new File("config.xlsx")));
   sheet = workBook.getSheetAt(0);
   evaluator = workBook.getCreationHelper().createFormulaEvaluator();
   formatter = new DataFormatter();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 static CellValue getCellValue(int r, int c) throws Exception {
  Row row = sheet.getRow(r);
  Cell cell = row.getCell(c);
  System.err.println(cell.getCellFormula());
  CellValue cellValue = evaluator.evaluate(cell);
  return cellValue;
 }

 static String getCellContent(int r, int c) throws Exception {
  Row row = sheet.getRow(r);
  Cell cell = row.getCell(c);
  System.err.println(cell.getCellFormula());
  String cellContent = formatter.formatCellValue(cell, evaluator);

  return cellContent;
 }

 public static void main(String[] args) throws Exception {
  System.out.println("G2:");
  System.out.println(getCellValue(1, 6));

  System.out.println("Cell values:");
  for (int r = 1; r < 4; r++) {
   System.out.println(getCellValue(r, 5));
  }
  System.out.println("Cell contents:");

  for (int r = 1; r < 4; r++) {
   System.out.println(getCellContent(r, 5));
  }
  workBook.close();
 }
}

在以下环境中:Java 8apache poi 4.0.0。它产生以下输出:

axel@arichter:~/Dokumente/JAVA/poi/poi-4.0.0$ java -cp .:./*:./lib/*:./ooxml-lib/* ReadExcelFormulaEvaluatorExample 
G2:
TODAY()-1
org.apache.poi.ss.usermodel.CellValue [43369.0]
Cell values:
VLOOKUP(F4,A:C,2,0)
org.apache.poi.ss.usermodel.CellValue [43362.0]
VLOOKUP(F4,A:D,4,0)
org.apache.poi.ss.usermodel.CellValue ["Period 10"]
SUMIFS(A:A,B:B,"<="&G2,C:C,">="&G2)
org.apache.poi.ss.usermodel.CellValue [10.0]
Cell contents:
VLOOKUP(F4,A:C,2,0)
9/19/18
VLOOKUP(F4,A:D,4,0)
Period 10
SUMIFS(A:A,B:B,"<="&G2,C:C,">="&G2)
10

顺便说一句:由于B:BC:C中的所有日期都按升序排列,因此C:C的结束日期与B:B的下一个开始日期之间没有任何间隔,公式

SUMIFS(A:A,B:B,"<="&G2,C:C,">="&G2)

可以替换为

INDEX(A:A,MATCH(G2,B:B,1))