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

I am writing a program that imports xlsx files using EventModel Apache POI (SAX / XSSF). I am almost ready, but I cannot get the correct dates.

I am parsing a cell with a date value

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

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

 DateUtil.getJavaCalendar(doble date, bool use1904windowing); 

What should I pass as use1904windowing to get the correct dates? I use false temporarily, as this gives me the correct dates in my test book, but I know that I have to read this value somewhere.

In xls binary, there is a DateWindow1904Record entry that I am reading using HSSF. What is its counterpart in XSSF? Or should I always use false ?

EDIT : @rgettman's answer pointed me to a solution, but it is not complete. In the event model, you do not have an xssfWorkbook object, and you cannot just get it from CWWorkbook ().

Instead, you can create a CTWorkbook directly from an 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(); 
+6
source share
2 answers

The code described in the EDIT section compiles, but always returns null CTWorkbookPr in POI 3.9 The code below parses the book 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(); 
+4
source

You can determine if the date format is 1904 in XSSF. Unfortunately, isDate1904() protected in the XSSFWorkbook . But there is a workaround made possible because XSSFWorkbook provides its basic XML bean using the getCTWorkbook() method.

 boolean isDate1904 = false; CTWorkbook internalWorkbook = xssfWorkbook.getCTWorkbook(); CTWorkbookPr workbookPr = internalWorkbook.getWorkbookPr(); if (workbookPr != null) { isDate1904 = workbookPr.getDate1904(); } 

This code examines the underlying XML beans to determine if the date1904 attribute is date1904 . It is also possible to set this flag with the same XML bean ( CTWorkbookPr ) using the setDate1904(boolean) method.

+3
source

All Articles