Search code examples
dateapache-poixssf

How to check if date uses 1904 windowing with Apache POI XSSF eventmodel


I am writing a program that imports xlsx files with Apache POI eventmodel (SAX/XSSF). I am almost done, but I can't get the correct dates.

I am parsing a cell with date value

<c r="D1" s="1">
    <v>41319.558333333334</v>
</c>

I am using the org.apache.poi.ss.usermodel.DateUtil class to get the date:

DateUtil.getJavaCalendar(doble date, bool use1904windowing);

What should I pass as use1904windowing to get correct dates? Temporarily I use false, as this gives me correct dates with my test workbook, but I know I should read that value from somewhere.

There is a record DateWindow1904Record in binary xls format, which I read using HSSF. What is its counterpart in XSSF? Or should I always use false?

EDIT: @rgettman answer pointed me to the solution, but it's not complete. In event model you do not have xssfWorkbook object and you can't just getCTWorkbook() of it.

Instead you can create CTWorkbook directly form InputStream:

OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg );
InputStream workbookXml = r.getWorkbookData();
CTWorkbook ctWorkbook = CTWorkbook.Factory.parse(workbookXml);
boolean isDate1904 = ctWorkbook.getWorkbookPr().getDate1904();

Solution

  • Code described in EDIT section compiles, but always return null CTWorkbookPr in POI 3.9 Code below actually parses workbook prefix:

    OPCPackage pkg = OPCPackage.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    InputStream workbookXml = r.getWorkbookData();
    WorkbookDocument doc = WorkbookDocument.Factory.parse(workbookXml);
    CTWorkbook wb = doc.getWorkbook();
    CTWorkbookPr prefix = wb.getWorkbookPr();
    boolean isDate1904 = prefix.getDate1904();
    pkg.close();