for(int i=0;i<wbs[0].getNumberOfSheets();i++){ sheet1 = wbs[0].getSheetAt(i); for (Cell cell : row) { switch(cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println("CELL_TYPE_STRING |"+cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)) { System.out.println("CELL_TYPE_NUMERIC |"+cell.getDateCellValue()); } else { System.out.println("CELL_TYPE_NUMERIC |"+cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.println("CELL_TYPE_BOOLEAN |"+cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA:
System.out.print("Cell.CELL_TYPE_FORMULA |"+cell.getCellFormula()+"|"); CellValue cellValue = evaluators[0].evaluate(cell); // 或者evaluators[0].evaluateFormulaCell(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.println(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.println(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(cellValue.getStringValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: break; // CELL_TYPE_FORMULA will never happen case Cell.CELL_TYPE_FORMULA: break; }// end of switch
break; default: System.out.println(); }// end of switch } }
原理
所以问题的关键是:
从CELL的FORMULA计算式获得何种格式的文件名,而POI系统会以该格式的文件名从 Formula Evaluator Environment获取对应的Evaluator