I tried to set formula in a cell using following three ways. 1. HSSFCell cell = sheet.createCell(1); 2. cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); 3. cell.setCellFormula("A2+B2"); Gives Error on line 2 1. HSSFCell cell = sheet.createCell(1,HSSFCell.CELL_TYPE_FORMULA); 2. cell.setCellFormula("A2+B2"); This works but for only once i.e if I try setCellFormula again it gives a ClassCastException line 458 in HSSFCell.java() 1. HSSFCell cell = sheet.createCell(1); 2. cell.setCellFormula("A2+B2"); This works perfectly
I am not developer:D,but I have an opinion about the problem. When I tried to call HSSFCell#setCellType(CELL_TYPE_STRING) to a cell having a "number value" and I fouund it causes Exception.It is reasonable because number value is already existed. HSSFCell has some metod to set value,setCellFormula(formulla string) and setCellValue(for each value types).and in thease methods,inner logic automatically changes cell's type to right value. So I feel,the method HSSFCell#setCellType() is no need to open as public method. The matter that the method is pubic, may make users confuse(as I did:D).The method should be "deprecated method".
I went throught the source code I find that setCellFormula(String formula) calls the setCellType(HSSFCell.CELL_TYPE_FORMULA) so, you are right because this method setCellType(HSSFCell.CELL_TYPE_FORMULA) is being called twice hence throws exception. Thanks for your reply..
this looks like a conversation that should have been on the list and has resolved itself.