Writing various Date Formats in Excel using POI

Using Apache POI we can write dates in various formats in an excel cell using CellStyle. Lets see ways of writing various date formats in excel using POI.

Lets follow the below steps
1. Create a workbook object
2. Create a XSSFSheet object
3. Now create a CreationHelper object (This is needed to style the date object)
4. Now create row/cells to write the date
5. Create CellStyle and apply the CreationHelper object to format the date in your formats.
example

Please Note that, if we do not provide any date formatter while writing date into cells, the date will be written as simple string or number. (Please see the example below)

Now lets see an example. We will write date in 3 formats
1st Cell – Write Date without any formatter
2nd Cell – Write Date using a CellStyle and formatter – format dd-mm-yyyy
3rd Cell – Write Date using another CellStyle formatter – format mm/dd/yyyy hh:mm:ss

Please note : It is important to create a new cell style from the workbook otherwise you can end up modifying the existing style and this will affect all the other cells that use that style.

Output
As you can see the results
1st Cell – Date was converted to Number and then written.
2nd Cell – Date was converted to the desired format.
3rd Cell – Date was converted to the desired format.

writing various date formats in excel using POI

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn