Reena Upadhyay Reena Upadhyay - 1 year ago 172
Apache Configuration Question

get format of date for date column in excel

I am using Apache POI 3.11 version for reading and writing an excel file in Java

In my input excel file, I have a column which contains date. Date can be in any format like say dd/mm/yyyy hh:mm or yy/mm/dd etc. While reading the file, I can get date in any format.

enter image description here

When I am creating the new excel file as output, I want to write date column. But I want to use the same format of date that was present in the input excel file.

Is there any way, by which I can get out the date format present in the input excel file (Above screen shot you can see the date was in mm/dd/yyyy hh:mm:ss PM format)

I do not want to use

function, because it returns me the complete style of cell including the font color, background color etc.
I just want the date format information of a cell that was present in the input excel file.

Way I am trying :

CellStyle outputStyle = wb.createCellStyle();
Font textFont = wb.createFont();
textFont.setFontHeightInPoints((short) 10);

if (DateUtil.isCellDateFormatted(icell)) {


It is writing the value of Date in the cell as number.

Answer Source

You don't have to use the whole CellStyle object returned by getCellStyle, only the DataFormat of the cell style returned by getDataFormat.

Dataformat format = cell.getCellStyle().getDataFormat();

and then when you write a new file


Alternatively, if this does not want to work, another approach is to use the CreationHelper class and the getDataFormatString method:

CreationHelper createHelper = outputWB.getCreationHelper();
CellStyle outputStyle = outputWB.createCellStyle(); 

Variable cell is the cell read from the file and variable newCell is the cell to be written into the new file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download