/** * * Returns the Formatted value of a cell as a <tt>String</tt> regardless * of the cell type. If the Excel FormatBase pattern cannot be Parsed then the * cell value will be Formatted using a default FormatBase. * * When passed a null or blank cell, this method will return an empty * String (""). Formula cells will be evaluated using the given * {@link HSSFFormulaEvaluator} if the evaluator is non-null. If the * evaluator is null, then the formula String will be returned. The caller * is responsible for setting the currentRow on the evaluator * * * @param cell The cell (can be null) * @param evaluator The HSSFFormulaEvaluator (can be null) * @return a string value of the cell */ public String FormatCellValue(Cell cell, FormulaEvaluator evaluator) { if (cell == null) { return(""); } CellType cellType = cell.CellType; if (evaluator != null && cellType == CellType.FORMULA) { try { cellType = evaluator.EvaluateFormulaCell(cell); } catch (Exception e) { throw new Exception("Did you forGet to set the current" + " row on the HSSFFormulaEvaluator?", e); } } switch (cellType) { case CellType.FORMULA: // should only occur if evaluator is null return(cell.CellFormula); case CellType.NUMERIC: if (DateUtil.IsCellDateFormatted(cell)) { return(GetFormattedDateString(cell)); } return(GetFormattedNumberString(cell)); case CellType.STRING: return(cell.RichStringCellValue.String); case CellType.BOOLEAN: return(cell.BooleanCellValue.ToString().ToUpper()); case CellType.BLANK: return(""); } throw new Exception("Unexpected celltype (" + cellType + ")"); }
/** * * Returns the Formatted value of a cell as a <tt>String</tt> regardless * of the cell type. If the Excel FormatBase pattern cannot be Parsed then the * cell value will be Formatted using a default FormatBase. * * When passed a null or blank cell, this method will return an empty * String (""). Formula cells will be evaluated using the given * {@link HSSFFormulaEvaluator} if the evaluator is non-null. If the * evaluator is null, then the formula String will be returned. The caller * is responsible for setting the currentRow on the evaluator * * * @param cell The cell (can be null) * @param evaluator The HSSFFormulaEvaluator (can be null) * @return a string value of the cell */ public String FormatCellValue(Cell cell, FormulaEvaluator evaluator) { if (cell == null) { return ""; } CellType cellType = cell.CellType; if (evaluator != null && cellType == CellType.FORMULA) { try { cellType = evaluator.EvaluateFormulaCell(cell); } catch (Exception e) { throw new Exception("Did you forGet to set the current" + " row on the HSSFFormulaEvaluator?", e); } } switch (cellType) { case CellType.FORMULA: // should only occur if evaluator is null return cell.CellFormula; case CellType.NUMERIC: if (DateUtil.IsCellDateFormatted(cell)) { return GetFormattedDateString(cell); } return GetFormattedNumberString(cell); case CellType.STRING: return cell.RichStringCellValue.String; case CellType.BOOLEAN: return cell.BooleanCellValue.ToString().ToUpper(); case CellType.BLANK: return ""; } throw new Exception("Unexpected celltype (" + cellType + ")"); }