Hello I'am using POI in order to read excels files from my users. In one of the cells, i've got this formula : IF(H24>0,((D24*(Q24))/((3600*H24)/I24))+IF(AND(N24>0,L24>0),(D24*L24*(Q24)/N24),0),0) The problem arrive when i've got a 0 value for N24. In this case, POI returns me an DIV/0 error, but i don't understand why .. because of the IF (N24>0) test. Anyway, I tried changing the formula and keep only the latest part like this : IF (N24>0,D24*L24*(Q24)/N24,0) => still doesn't work. To Evaluate the formula I use : FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); And then i do a switch.
Can you try stepping into the formula evaluator in a debugger, and see why the evaluation is coming out that way?
I made some test. When changed : IF (N24>0,D24*L24*(Q24)/N24,0) by IF ((N24*1)>0,D24*L24*(Q24)/N24,0) it works. Then trying to explain why : I checked the excel file : The type in Standard Number Then I checked the code and i made cell.getCellType() which return a String ... so because of the string return the test N24 > 0 was always true ... so that's why i had a DIV/0 error. The thing i don't understand is why the getCell returns a string ... in place of a number.
We found a Bypass. When using the method evaluateFormulaCell, it was impossible to get the catchedValue. By using the valuator.evaluate(cell), we still have the error but we can get the catchedValue.
Thanks for your response and sorry ... I found an error of programation. To get a long, the developer used this method : public static Object getCellValue(Row row, int index) { if (row == null || row.getCell(index) == null) { return null; } Object retour = null; Cell cell = row.getCell(index); cell.setCellType(Cell.CELL_TYPE_STRING); String val = cell.getStringCellValue().trim(); if (val != null && !"".equals(val)) { retour = cell.getStringCellValue(); } return retour; } The gulty is : cell.setCellType(Cell.CELL_TYPE_STRING); ....
The javadocs for cell.setCellType do warn you not to do that! I'd suggest switching to using a missing cell policy on the cell fetch, and using DataFormatter to give the string Glad to hear that the formula parser wasn't the issue after all!