You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by David Sills <DS...@datasourceinc.com> on 2012/07/26 18:37:17 UTC

Date formatting doesn't seem to work with POI 3.0.2 and Excel 2010

All:

 

I'm a newbie at using POI (using 3.0.2 mostly because that's the latest
Spring works with) and it seemed really sensible until I hit this
roadblock. I can create a cell just fine (working with the HSSF classes,
as that's the form appropriate for my client), but when I try to create
a date-formatted cell, no formatting seems to work at all! I've tried
the built-in date/time formats in HSSFDataFormat, custom formats, the
works. All I get is something like "40973.28833" every time. The cell
comes up as a General cell, not a Date cell (or even, as I set it at one
point, as a numeric cell - as you can see below, I tried also not
setting it at all, and that didn't help either).

 

My code looks like this:

 

                     HSSFCellStyle cellStyle =
workbook.createCellStyle();

                     HSSFDataFormat formatter =
workbook.createDataFormat();

                     short idx = formatter.getFormat(dateFormat);

                     if (idx > -1)

                     {

                           cellStyle.setDataFormat(idx);

//                         cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

                           cell.setCellValue((Date) o);

                     }

                     else

                     {

                           String s = dataCell.getValue(rowcount, data);

                           cell.setCellType(HSSFCell.CELL_TYPE_STRING);

                           cell.setCellValue(new HSSFRichTextString(s));

                     }

 

 

Has anyone any ideas? I have looked through the mailing list archives
and couldn't see anyone else having this problem, so it must be
something I'm doing wrong. I would greatly appreciate any help anyone
can give me. I can obviously format the date as a String, but then it
won't sort correctly in Excel, I wouldn't imagine.

 

Thanks!

 

David Sills

 


Re: Date formatting doesn't seem to work with POI 3.0.2 and Excel 2010

Posted by Nick Burch <ni...@alfresco.com>.
On Thu, 26 Jul 2012, David Sills wrote:
>                     HSSFCellStyle cellStyle =
> workbook.createCellStyle();
>
>                     HSSFDataFormat formatter =
> workbook.createDataFormat();
>
>                     short idx = formatter.getFormat(dateFormat);
>                     if (idx > -1)
>                     {
>                           cellStyle.setDataFormat(idx);
> //                         cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
>                           cell.setCellValue((Date) o);
>                     }

Aren't you missing the code to apply the cell style to the cell?

eg cell.setCellStyle(cellStyle) ?

Nick

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