我创建了一个简单的电子表格来测试这个问题。在电子表格顶部计算百分比。在不同数字列的总和下方包括总和乘以计算百分比的计算(递归公式)。
这在 EXCEL 中运行良好。
在 POI 中,当计算单元格时,它会抛出一个未记录的 FormulaError 代码 (-60 )。
有没有办法确定这个错误代码是什么以及如何解决/修复它?
代码:
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/
// Read the Excel template from the local hard drive
String filename = "c:\\temp\\RecurssionTest.xls";
// Create the control number and format
Workbook wb = new HSSFWorkbook(new FileInputStream(filename));
wb.setForceFormulaRecalculation(true);
//Get the row and cell (zero index)
int rindex = 0;
int cindex = 0;
//Get the Notes Fields worksheet
Sheet ws = wb.getSheet("Sheet1");
//String fields[] = {"Position1","Position","PositionWC","Positionprefix","raterwc","Client_Name","State","Client_Division"};
cellSetValNum(ws, rindex, cindex, 120);
// Write the output to a file
String excelOutput = "C:\\temp\\UpdatedRT.xls";
FileOutputStream fileOut = new FileOutputStream(excelOutput);
wb.write(fileOut);
System.out.println("Formula Errors are: ");
FormulaError fevals[] = FormulaError.values();
for (FormulaError formulaError : fevals) {
System.out.println("formula error val: " + formulaError.getCode());
}
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
Sheet sheet = wb.getSheet("Sheet1");
System.out.println("processing sheet: " + sheet.getSheetName());
for(Row r : sheet) {
for(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
System.out.println("Recalcing: "+r.getRowNum()+c.getColumnIndex() + "in "+ sheet.getSheetName());
System.out.println("Cell Type before is: "+c.getCellType());
//System.out.println("Cell value before is:" +c.getNumericCellValue());
evaluator.evaluateFormulaCell(c);
evaluator.evaluateAll();
System.out.println("Cell Formula is: "+ c.getCellFormula());
System.out.println("Cell Type after is: "+c.getCellType());
//System.out.println("Cell Value After is: "+c.getNumericCellValue());
if(r.getRowNum()== 11){
System.out.println("This cell has an error");
System.out.println("Cell Error Value is: "+c.getErrorCellValue());
}
System.out.println("");
}
}
}
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
堆栈跟踪:
start set cell num
0 120.0
End set cell num
Formula Errors are:
formula error val: 0
formula error val: 7
formula error val: 15
formula error val: 23
formula error val: 29
formula error val: 36
formula error val: 42
processing sheet: Sheet1
Recalcing: 20in Sheet1
Cell Type before is: 2
Cell Formula is: A1*A2
Cell Type after is: 2
Recalcing: 100in Sheet1
Cell Type before is: 2
Cell Formula is: A3*A12
Cell Type after is: 2
Recalcing: 110in Sheet1
Cell Type before is: 2
Cell Formula is: SUM(A6:A11)
Cell Type after is: 2
This cell has an error
Cell Error Value is: -60