Well, as you have just discovered, to Excel a date is nothing more than a
large number. To actually see the date formatted in the familar fashion, you
need to create an apply a style to the cell and in order to best show you
how to do that, I really needed to know which version of the file format you
were targetting, binary with HSSF, OOXML with XSSF or both with the classes
in the SS stream.

As I do not, I will have to explain with an example that uses the HSSF
stream - I have not tested this exact piece of code as I do not have an
editor open currently but am pretty confident it will work;

// Just some basic code to create a new workbook, add a sheet,
// a row to the sheet and a cell to the row.
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Date Test");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);

// Now to use your code to set the date value into the cell.
cell.setCellValue(Calendar.getInstance());

HSSFCellStyle dateStyle = workbook.createCellStyle();
HSSFDataFormat formatter = workbook.createDataFormat();
// This should work as a date format. PLay with the String and
// see what happens.
dateStyle.setDataFormat(formatter.getFormat("dd/mm/yyyy"));

cell.setCellStyle(dateStyle);

FileOutputStream fos = new FileOutputStream(new File(".............."));
workbook.write(fos);

and that ought to do the trick. Have a look here -
http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
- and you wuill see a list of the buil-in formats that ship as standard with
Excel. Obviously, you can create your own and if you want to so this, it is
often best to experiment directly with Excel.

Yours

Mark B




phucbui wrote:
> 
> This is my code
> cell.setCellValue(Calendar.getInstance());
> And this is result :confused:
> 40296.92606
> I have read many instructions to get correctly  date format from available
> cells, but I do not know how to create a cell with correct date format
> yet.
> Please help me that :-)
> 
> Phuc Bui
> 

-- 
View this message in context: 
http://old.nabble.com/Problem-with-date-tp28389526p28393066.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to