我正在尝试计算 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();
最佳答案
抱歉,我无法重现您的问题。我已经下载了你的文件。然后运行以下代码时:
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 8
, apache 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:B
中的所有日期起和C:C
按升序排列,并且 C:C
中的结束日期之间没有任何间隙接下来从 B:B
开始,公式
SUMIFS(A:A,B:B,"<="&G2,C:C,">="&G2)
可以替换为
INDEX(A:A,MATCH(G2,B:B,1))
.
关于java - Apachie POI 中的 FormulaEvaluator 仅返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508537/