Donahue, Michael wrote:
Igor -

I haven't had an opportunity to use HSSFDateUtil on a cell of type
CELL_TYPE_FORMULA, but I believe all cells have an assigned cell style and
all cell styles have an assigned data format.  So it would seem that you
could just copy the nested if (HSSFDateUtil.isCellDateFormatted(cell)) from
my example to check the data format of any cell type.  If it's a date data
format, then process it as a date, else handle it as a formula.
(I think you mean "else handle it as a number", but regardless...)

This is more or less what I'm doing in our code here.

   private String cellToString(HSSFCell cell) {
       switch (cell.getCellType()) {
           // other cell types ...
           case HSSFCell.CELL_TYPE_NUMERIC:
               return cellValueToString();
           case HSSFCell.CELL_TYPE_FORMULA:
               return cell.getCellFormula() + "=" + cellValueToString();
       }
   }

   private String cellValueToString(HSSFCell cell) {
       if (HSSFDateUtil.isCellDateFormatted(cell)) {
           return DATE_FORMAT.format(cell.getDateCellValue());
       } else {
           return String.valueOf(cell.getNumericCellValue());
       }
   }

It works "well enough". I would rather format the value using the real date format that the sheet had configured, but HSSFDataFormat returns formats which bear no relationship to the SimpleDateFormat class, and I have no current interest in using regular expressions to mangle them enough to make them usable. :-)

I also wonder: what if the formula result is a boolean? Is that possible in Excel? What if it's a string? I should also check for string formats, I suppose (format 0x31), but there isn't a format for booleans.

What would have made life a whole lot easier is a method to get the value of the cell as an Object.

And who knows what "General" means... if it's a number format, it certainly isn't clear how it differs from the other number formats.

Daniel


--
Daniel Noll

Nuix Australia Pty Ltd
Suite 79, 89 Jones St, Ultimo NSW 2007, Australia
Phone: (02) 9280 0699
Fax:   (02) 9212 6902

This message is intended only for the named recipient. If you are not
the intended recipient you are notified that disclosing, copying,
distributing or taking any action in reliance on the contents of this
message or attachment is strictly prohibited.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/

Reply via email to