Hi,

I need to generate an excel file which contains dates prior to 1900.

It doesn't seem to work as the code below shows (tested with version 3.9).

What am I missing ?

Thank you,

--
Pierre-Henri

public class Main {
    public static void main(String[] args) throws Exception {
        GregorianCalendar calendar1901 = new GregorianCalendar(1901, 1, 1);
        GregorianCalendar calendar1889 = new GregorianCalendar(1889, 1, 1);

        Date year1901 = calendar1901.getTime();
        Date year1889 = calendar1889.getTime();

        Workbook wb = new HSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet("my sheet");
        Row row = sheet.createRow(0);

        // OK : this ends up correctly in the excel file
        CellStyle cellStyle = wb.createCellStyle();

cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yy"));
        Cell cell = row.createCell(0);
        cell.setCellValue(year1901);
        cell.setCellStyle(cellStyle);

        // KO : this ends up with the value "-1" in the excel file
        cellStyle = wb.createCellStyle();

cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yy"));
        cell = row.createCell(1);
        cell.setCellValue(year1889);
        cell.setCellStyle(cellStyle);

        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

Reply via email to