The method you are looking for is Cell.getCachedFormulaResultType - for a formula cell that tells you the type of formula result
In this case, the code might look something like this:
private void handleCell(int type, Cell cell) { if (type == HSSFCell.CELL_TYPE_STRING) { ar.add(cell.getStringCellValue()); } else if (type == HSSFCell.CELL_TYPE_NUMERIC) { ar.add(cell.getNumericCellValue()); } else if (type == HSSFCell.CELL_TYPE_BOOLEAN) { ar.add(cell.getBooleanCellValue()); } else if (type == HSSFCell.CELL_TYPE_FORMULA) { // Re-run based on the formula type handleCell(cell.getCachedFormulaResultType(), cell); } else { ar.add(""); } } public void handleSheet(Sheet sheet) { for (Row row : sheet) { for (Cell cell : row) { handleCell(cell.getCellType(), cell); } } }
Note that iterators provide only the cells that are defined in the file, so there will be spaces if the cells have never been used. If you need every cell, including missing ones, see Iterating vs Fetching docs
source share