title: Apache POI - Read a cell formatted by TEXT() formula
body: <p>I have an EXCEL file with dates in it. They are formatted as TEXT, for example: <code>=TEXT(TODAY(); "yyyy-MM-dd")</code></p>
  
  <p>In EXCEL the date is correctly formatted as text, but when I read the cell with Apache POI, it will return the numeric value.
  <strong>Why? Why does POI not read the formatted text value?</strong></p>
  
  <p>I don't want to format the date in my JAVA application, because the EXCEL file should define the format (it could be different for each value).</p>
  
  <p>Here's my code for reading the cell value:</p>
  
  <pre><code>private static String getString(Cell cell) {
   if (cell == null) return null; 
  
   if (cell.getCellTypeEnum() != CellType.FORMULA) { 
    switch (cell.getCellTypeEnum()) { 
     case STRING: 
      return cell.getStringCellValue().trim(); 
     case BOOLEAN: 
      return String.valueOf(cell.getBooleanCellValue());
     case NUMERIC: 
      return String.valueOf(cell.getNumericCellValue()); 
     case BLANK: 
      return null; 
     case ERROR: 
      throw new RuntimeException(ErrorEval.getText(cell.getErrorCellValue())); 
     default: 
      throw new RuntimeException("unexpected cell type " + cell.getCellTypeEnum());
    }
   } 
   FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
   try { 
    CellValue cellValue = evaluator.evaluate(cell); 
    switch (cellValue.getCellTypeEnum()) { 
     case NUMERIC: 
      return String.valueOf(cellValue.getNumberValue());
     case STRING: 
      return cellValue.getStringValue().trim(); 
     case BOOLEAN: 
      return String.valueOf(cellValue.getBooleanValue()); 
     case ERROR: 
      throw new RuntimeException(ErrorEval.getText(cellValue.getErrorValue())); 
     default: 
      throw new RuntimeException("unexpected
  cell type " + cellValue.getCellTypeEnum()); 
    } 
   } catch (RuntimeException e) { 
    throw new RuntimeException("Could not evaluate the value of " + cell.getAddress() + " in sheet " + cell.getSheet().getSheetName(), e);
   }
  }
  </code></pre>