I want to disable all formulas on my excel sheet. I wrote this code as a test for only one cell. Although everything seen fine, my B3 cell remains as formula cell. I searched a lot but did not find solution. What is problem of my code or what you recommend?
public static void formulaDisabler() throws IOException, InvalidFormatException {
FileInputStream file = new FileInputStream("C:\\Users\\Farid\\Desktop\\test.xlsx");
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell;
cell = row.getCell(cellReference.getCol());
if (cell != null) {
switch (evaluator.evaluateInCell(cell).getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
System.out.println(cell.getErrorCellValue());
break;
// CELL_TYPE_FORMULA will never occur
case Cell.CELL_TYPE_FORMULA:
break;
}
} else {
System.out.println("cell is null");
}
}