1

I am trying to use Apache's Java POI to add Dates to a spreadsheet. However, I am ending up with the dates in the date-formatted cells not being recognized as dates by the cell but as Strings. Yet, when I open the spreadsheet subsequently, nothing more than it takes is a click into the cell - i.e. editing it while not even making an effective change - and the date format is recognized automatically. How can I make my program conduct this very last step without me needing to interfere? Thank you very much, in advance!

    CreationHelper creationHelper = wb.getCreationHelper();
    XSSFCellStyle cellStyleDate = wb.createCellStyle();

    LocalDate start = LocalDate.of(2000, 1, 1);
    LocalDate end = LocalDate.of(2000, 12, 31);

    cellStyleDate.setDataFormat(creationHelper.createDataFormat().getFormat("dd.mm.yyyy"));
    int i = 1;
    for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {

        // wb.getSheetAt(0).createRow(i).createCell(0).setCellValue(date);
        wb.getSheetAt(0).createRow(i).createCell(0).setCellValue(date.toString());
        // wb.getSheetAt(0).createRow(i).createCell(0).setCellValue(date.getDayOfMonth() + "." + date.getMonthValue() + "." + date.getYear());

        wb.getSheetAt(0).getRow(i).getCell(0).setCellStyle(cellStyleDate);
        i++;
    }

2 Answers 2

6

As of it is documented there are two possible methods for setting a date value. setCellValue(java.util.Calendar value) and setCellValue(java.util.Date value). Using java.time.LocalDate is not supported until now.

So you must convert the LocalDate into java.util.Date before setting as cell value.

Example:

import java.io.*;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Date;
import java.time.LocalDate;
import java.time.ZoneId;

import java.awt.Desktop;

class LocalDateTest {

 public static void main(String[] args) {
  try {

   Workbook wb = new XSSFWorkbook();
   Sheet sheet = wb.createSheet("Sheet1");

   CreationHelper creationHelper = wb.getCreationHelper();
   CellStyle cellStyleDate = wb.createCellStyle();

   LocalDate start = LocalDate.of(2000, 1, 1);
   LocalDate end = LocalDate.of(2000, 12, 31);

   cellStyleDate.setDataFormat(creationHelper.createDataFormat().getFormat("dd.mm.yyyy"));
   int i = 1;
   for (LocalDate localdate = start; localdate.isBefore(end.plusDays(1)); localdate = localdate.plusDays(1)) {
    Date date = Date.from(localdate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    wb.getSheetAt(0).createRow(i).createCell(0).setCellValue(date);
    wb.getSheetAt(0).getRow(i).getCell(0).setCellStyle(cellStyleDate);
    i++;
   }

   OutputStream out = new FileOutputStream("LocalDateTest.xlsx");
   wb.write(out);
   wb.close();

   System.out.println("Done");
   File outputfile = new File("LocalDateTest.xlsx");
   Desktop.getDesktop().open(outputfile);


  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }
 }
}

The above is true for Apache POI versions of 2016. The current apache poi 5.2.2 provides Cell.setCellValue(java.time.LocalDate value) as well as Cell.setCellValue(java.time.LocalDateTime value).

1
  • Works perfectly now! Regarding the limitations of the current documentation, you are a hundred percent right. Thank you very much! I guess my lesson for today is, always going for the newest is not necessarily better, especially when those things are not (yet) supported. Moreover, thank you very much for the elaborated example. Wish you a nice day!
    – noai
    Jul 21, 2016 at 8:17
5

setCellValue is now also defined for LocalDate and LocalDateTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.