2

I am facing one strange issue. I have written a program in java using apache poi 5.2.2 which reads csv file and writes in to excel sheet(.xlsx).In that csv file there is one date column, the format is in mm-dd-yyyy(04-25-2022). my program converts this format to d/mmm/yyyy format. When I run my program it creates new xlsx sheet. But when I open this file the date column still shows in mm-dd-yyyy(04-25-2022) format. But if I double click on that cell it changes it to d/mmm/yyyy (25/Apr/2022). Is there any function in java? Below is my code. Is there any solution or this is excel issue.

String value = null;
CellStyle style = workBook.createCellStyle();
CreationHelper createHelper = workBook.getCreationHelper();
style.setDataFormat(createHelper.createDataFormat().getFormat("d/MMM/yyyy"));

BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
    String str[] = currentLine.split(",");
    RowNum++;

    XSSFRow currentRow=sheet.createRow(RowNum);
    for (int i = 0; i < str.length; i++) {
        value = str[i];
        Cell cell = currentRow.createCell(i);
        cell.setCellValue(value);
        cell.setCellStyle(style);
    }
 }

FileOutputStream fileOutStream =  new FileOutputStream(xlsxFileAddress);
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Prashant Naik
  • 105
  • 1
  • 8
  • 1
    I hope this will help you. It works for me. https://stackoverflow.com/questions/33889667/poi-date-format-dd-mmm-yy-not-recognized-as-a-excel-format – vu minh huy Sep 07 '22 at 07:40
  • 2
    Dates should be stored as numbers - not as strings - POI has a setCellValue(java.util.Date) - this will convert the date to right numeric format. There is equivalent support for java.time.LocalDate – PJ Fanning Sep 07 '22 at 12:36

0 Answers0