poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "May, James" <James....@FMR.com>
Subject Re: Retrieve formatted cell value
Date Fri, 18 Jul 2008 19:37:42 GMT

Hi Marc, Andy;

I may have a solution to the problem. If I understand correctly, you'd
like to get the value of a cell as a String and have the string
formatted appropriately based on the cell's data-format-string. I've
written a class I call HSSFDataFormatter (not sure that is the most
appropriate name, but anyway...). HSSFDataFormatter returns a formatted
string for numeric type cells. For non-numeric cells, it returns

  HSSFDataFormatter formatter = new HSSFDataFormatter();
  HSSFCell cell = row.getCell(0);
  String formattedStr = formatter.formatCellValue(cell);

The code above returns a formatted string based on the cell's type and
data format string:

Numeric cell:  formattedStr =  "$100.00" or  "25%" or "Oct 21, 2009 1:30
PM" etc...
String cell:   formattedStr = cell.getRichStringCellValue().toString();
Boolean cell:  formattedStr =
Formula cell:  formattedStr = cell.getCellFormula();
Blank cell:    formattedStr = ""

You can also pass in a FormulaEvaluator so that formula cells will be
evaluated and then formatted:

  String formattedStr = formatter.formatCellValue(cell, evaluator);

I submitted a patch
(https://issues.apache.org/bugzilla/show_bug.cgi?id=45404) for this. You
can check out the code in the attachment on bugzilla. If you think this
would be beneficial in the API, you can vote for this bug.


James May
Sr. Software Engineer

To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org

View raw message