この問題をテストするために、簡単なスプレッドシートを作成しました。パーセンテージは、スプレッドシートの上部で計算されます。数値の異なる列の合計の下には、合計に計算されたパーセンテージを掛けたものの計算が含まれます(再帰式)。
これは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