0

The date format created by POI can't be recognized by the system. My codes are listed below:

CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
short dateFormat = createHelper.createDataFormat().getFormat("MM/dd/yyyy");
cellStyle.setDataFormat(dateFormat);
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());

However, if I open the excel my java code created, I would see that the cell is in Custom format with "mm/dd/yyyy" type. And the cell is aligned to the left of the cell and our client's system can't read the data in the cell and update this data to their system.

However, if I double click on the cell and don't make any changes, but press "Enter" after that, the cell is still in Custom format with "mm/dd/yyyy" type, but it is aligned to the right of the cell now. And at this time, my client's system can read the data in the cell and upload the data to the system.

It seems like that the cell style my java code generated is different from the one excel generated. Can anyone help me with this?

Elton
  • 23
  • 7
  • 2
    The example works for me. Which POI version are you using? – JensS Aug 28 '17 at 16:50
  • 1
    If a "date" is aligned to the left in the `Excel` cell and is not readable as a date, then it is text data instead of numeric date data. But I cannot think any reason why `cell.setCellValue(new Date());` should produce text data instead of numeric date data. So I suspect the code you are showing here is not the one which produces the described behavior. Please show a [mcve](https://stackoverflow.com/help/mcve) to reproduce your problem. – Axel Richter Aug 29 '17 at 07:27
  • Yes, you are right. I was using the birth date of the person for the cell and I changed it to "new Date()" so that the code can be less. And you really helped me with it. The "date" is aligned to the left because they were read as string format. Thank you so much! – Elton Aug 29 '17 at 16:03

0 Answers0