Been able to play with Office 2010 on a colleagues laptop this morning and I
have found out that the format string is the critical part of the whole
operation.
If you want to repeat what I did, follow these steps.
Open a new, blank, workbook with Excel.
Select a cell on a sheet and right click it.
On the pop-up menu that appear select Data Format.
Choose Date in the l;eft hand combo box and pick one of the formats from the
right hand option box.
Click OK
Now, re-select that same cell, open the Data Format pop-up again and choose
Custom from the list in the left hand combo box.
You ought o see the data format string displayed in the field at the top of
the screen; on that laptop it looked like this - [$-809]dd mmmm yyyy;@
Copy this to something like Notepad.
Now, if you use these format string within POI, when you open the resulting
workbook, the type of the cell should be Date.
Try this code to see if iot works for you - this will also test how
standardised the formatting strings are of course.
public DateTest(String filename) throws IOException {
DataFormat format = null;
CellStyle cellStyle = null;
Workbook workbook = null;
Sheet sheet = null;
Row row = null;
Cell cell = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
StringBuilder refersToFormula = null;
if(filename.endsWith(".xlsx")) {
workbook = new XSSFWorkbook();
} else {
workbook = new HSSFWorkbook();
}
try {
sheet = workbook.createSheet("Date Test");
row = sheet.createRow(0);
cell = row.createCell(0);
cellStyle = workbook.createCellStyle();
format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("[$-809]dd mmmm
yyyy;@"));
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());
fos = new FileOutputStream(filename);
bos = new BufferedOutputStream(fos);
workbook.write(bos);
}
finally {
if(bos != null) {
bos.close();
}
}
}
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Set-cell-type-to-Date-tp5537937p5540647.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]