Bug 46673 - How to get formatted value from cell?
Summary: How to get formatted value from cell?
Status: RESOLVED INVALID
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-08 07:24 UTC by geetha
Modified: 2009-02-08 10:48 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description geetha 2009-02-08 07:24:45 UTC
Thanks for giving this product to us.

In my project, i want to convert the xls(or xlsx file) into CSV file.

I dont know how to get the formated value from the cell

   1. If the numeric cell have "$12,000" (With format) but i get only the value (12000).  
   2. If the Date cell "12-May" but i get only number (23455).

I missed some thing in code.so plz anybody help me. how to get the formatted value from the cell.

Here my code:

    WorkbookFactory wbFact = new WorkbookFactory();
    Workbook wbs = wbFact.create(new FileInputStream(file));
    int noOfSheets = wbs.getNumberOfSheets();
    FormulaEvaluator evaluator = wbs.getCreationHelper().createFormulaEvaluator();
    String csvString = "";   
    Sheet sheet = wbs.getSheetAt(sheetNo); 
    boolean isFirstRow = true;
    for (Iterator rit = sheet.rowIterator(); rit.hasNext(); )
    {
      if(!isFirstRow)
      {
        csvString += "\r\n";
      }
      Row row = (Row)rit.next();
      boolean isFirstCell = true;
      for (Iterator cit = row.cellIterator(); cit.hasNext(); )
      {
        if(!isFirstCell)
        {
          csvString += ",";
        }
        Cell cell = (Cell)cit.next();
        String val = "";
        switch (cell.getCellType())
        {
          case Cell.CELL_TYPE_BOOLEAN:
              val = String.valueOf(cell.getBooleanCellValue());
              break;
          case Cell.CELL_TYPE_NUMERIC:
              val = String.valueOf(cell.getNumericCellValue());
              break;
          case Cell.CELL_TYPE_STRING:
              val = String.valueOf(cell.getStringCellValue());
              break;
          case Cell.CELL_TYPE_BLANK:
              break;
          case Cell.CELL_TYPE_ERROR:
              break;
          case Cell.CELL_TYPE_FORMULA:
              val = evaluator.evaluate(cell).formatAsString();
              break;
        }
        csvString += "\"" + val + "\"";
        isFirstCell = false;
      }
      isFirstRow = false;
    }
    return csvString;   
  }
Comment 1 Nick Burch 2009-02-08 10:48:03 UTC
Firstly, please do not use bugzilla for use questions, use the mailing list for that. Bugzilla is only for bugs

Secondly, look at the various HSSF formatter classes, as by default poi returns the raw values stored in the file