【发布时间】:2018-01-30 06:11:06
【问题描述】:
你好,我遇到了一个错误
无法从错误公式单元格中获取文本值
同时获取具有公式的单元格的值。我正在使用 Apache poi 3.17
下面是我的代码:-
public ArrayList<String> excelread(int sheetnum, int rownum, int lastcell) throws IOException{ // to read the expected values from the excel sheet
ArrayList<String>expected= new ArrayList <String>();
File src= new File(filepath); //Writing in excel
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh=wb.getSheetAt(sheetnum);
XSSFRow row=sh.getRow(rownum);
XSSFFormulaEvaluator evaluator= wb.getCreationHelper().createFormulaEvaluator();
for (int currentcell=1;currentcell<=lastcell;currentcell +=2){
XSSFCell cell= row.getCell(currentcell);
evaluator.evaluateFormulaCell(cell);
String expectedresult = sh.getRow(rownum).getCell(currentcell).getStringCellValue(); // getting the value from a particular row and cell
expected.add(expectedresult);
}
wb.close();
return expected;
}
【问题讨论】:
-
错误很明显,不是吗?
cell中的公式计算结果为错误。我建议使用DataFormatter来获取单元格内容,如Getting the cell contents 所示。否则,您需要在获取内容之前始终检查CellType。
标签: java excel-formula apache-poi