views:

179

answers:

1

Hello everyone,

I have to create an excel file with POI-3.2-Final (can't upgrade to 3.5+, for retro compatibility issues) and for each row, i have to format a date in col A.

Based on the method names, i thought the way to do it was :

short dateFormat = workbook.createDataFormat().getFormat("dd/MM/yyyy HH:mm");
...
cell = row.createCell(0);
cell.setCellValue(new Date());
cell.getCellStyle().setDataFormat(dateFormat);

but, funny thing actually, it applies this style to ALL cells.

So, keeping in mind that i can't switch API (no upgrade, no csv, no jexcel), is there a way to achieve what i want ?

Thanks

+1  A: 

Your problem is that you are changing the default cellStyle!
cell.getCellStyle() gives you the style that is currently set on that cell which usually is the default style if you haven't changed it yet.
In order to make this work as you expect you have to create a new CellStyle object from your dateFormat and set that to the cell like this:

dateStyle = workbook.createCellStyle();
...
dateStyle.setDataFormat(dateFormat);
cell.setCellStyle(dateStyle);

Also make sure that you create that style object only once and reuse it for all cells that you want to have that style.

Turismo
ahh great ! thanks, really !
Maxime ARNSTAMM