poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon <aoz...@dsl.pipex.com>
Subject Re: creating a cell with date/time formatting
Date Mon, 15 May 2006 15:18:24 GMT

Tim Ellis <tim.ellis <at> emergint.com> writes:

> 
> I have reviewed the POI documentation and numerous posts regarding this issue
> and have tried every iteration presented; something is still missing, 
because I
> cannot create a cell that shows up as Date or Time formatted when I open the
> file in Excel.  I am using distribution poi-bin-2.5.1-final-20040804.zip with
> JDK 1.4.2.
> 
> The following code snippet yields a new cell, position 0, in new row 'r' that
> has a floating value when the file is opened in Excel; selecting the 
specified
> Time formatting type displays the intended cell value (17:15).
> 
> //__BEGIN_SNIPPET___
> 
> //...establish workbook and sheet
> 
> Date testDate = new Date();
> SimpleDateFormat fmtTime = new SimpleDateFormat("HH:mm", Locale.US);
> HSSFDataFormat tfmt = wb.createDataFormat();
> HSSFCellStyle cs = wb.createCellStyle();
> 
> //...establish row within sheet
> 
> c = r.createCell((short)0); //time
> testDate = fmtTime.parse("17:15");
> c.setCellValue(HSSFDateUtil.getExcelDate(testDate));
> cs.setDataFormat(tfmt.getFormat("13:30"));
> //cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("HH:mm"));
> c.setCellStyle(cs);
> 
> //__END_SNIPPET___
> 
> The same thing (floating value that can be correctly reformatted) happens 
for a
> Date format of "*3/14/2001" in Excel.  If anyone has a suggestion or simply 
an
> URL for a comprehensive set of examples, I would be much obliged.  Thanks in
> advance.
> 
> Tim
> 


I too am having problems setting a cell with a date format. I have the 
following code:

 _dateCellStyle = getDateCellStyle();
 cell.setCellValue(HSSFDateUtil.getExcelDate(dt));
 cell.setCellStyle(_dateCellStyle);
 boolean b = HSSFDateUtil.isCellDateFormatted(cell);

Where getDateCellStyle() is as below.

    private HSSFCellStyle getDateCellStyle()
    {
        HSSFCellStyle cellStyle = _wb.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("dd.MM.yyyy"));
        return cellStyle;
    }

Now b above is always 'false' - how can this be after I had just set it as a 
date format?

Thanks

Jon


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


Mime
View raw message