Bug 47411 - xlsx files with date values don't display properly in Mac Excel 2008 (due to different defaults, apparently)
Summary: xlsx files with date values don't display properly in Mac Excel 2008 (due to ...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Mac OS X 10.4
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-23 08:58 UTC by Leif Nelson
Modified: 2009-06-29 07:53 UTC (History)
0 users



Attachments
Patch to XSSFWorkbook.java from poi 3.5-beta6 (718 bytes, text/plain)
2009-06-23 08:58 UTC, Leif Nelson
Details
TestPoiDate.java - sample code exhibiting behavior (2.37 KB, text/plain)
2009-06-23 09:00 UTC, Leif Nelson
Details
Excel 2008 without the workbookPrTag in workbook.xml (this is showing the issue) (147.26 KB, image/png)
2009-06-23 09:06 UTC, Leif Nelson
Details
Excel 2008 with added workbookPrTag in workbook.xml (this is showing the fix) (143.58 KB, image/png)
2009-06-23 09:06 UTC, Leif Nelson
Details
Excel 2007 with workbookPrTag - This is showing that Excel 2007 works the same either way. (206.78 KB, image/png)
2009-06-23 09:07 UTC, Leif Nelson
Details
Excel 2007 without workbookPrTag - This is showing that Excel 2007 defaults to 1900 date formatting even with the workbookPr tag missing. (186.04 KB, image/png)
2009-06-23 09:08 UTC, Leif Nelson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Leif Nelson 2009-06-23 08:58:13 UTC
Created attachment 23853 [details]
Patch to XSSFWorkbook.java from poi 3.5-beta6

When I create a spreadsheet using XSSFWorkbook with date values in them, and open the file on Mac Excel 2008, all of the date values are off by exactly 4 years.  I have done some investigation, and I believe the issue is due to differing defaults when the workbook.xml file has a missing <workbookPr> tag (which seems to happen with all xlsx files created via XSSFWorkbook).  According to the ECMA-376 docs, on page 2292, it talks about "1900" vs "1904 backward compatibility" for dates in xlsx files, and I believe the "lack" of a <workbookPr> tag showing the different defaults for Windows Excel 2007 and Mac Excel 2008.

If I simply add the following code after workbook creation, the <workbookPr> tag is added to the workbook.xml file, and the dates show properly on Mac Excel 2008, and Windows Excel 2007.

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

XSSFWorkbook workbook = new XSSFWorkbook();
if (workbook.getCTWorkbook().getWorkbookPr() == null) {
  // this code causes a <workbookpr> element to be written to the workbook.xml 
  // file.  If this mode isn't set, then Mac
  // Excel 2008 defaults to date1904 mode whereas Windows Excel 2007  
  // defaults to date1900 mode.
  CTWorkbookPr workbookPr = CTWorkbookPr.Factory.newInstance();
  workbookPr.setDate1904(false);
  workbook.getCTWorkbook().setWorkbookPr(workbookPr);
}


I will attach some screenshots and example code showing the differences between display of dates on Excel 2007/Excel 2008.  Also, I believe the patch to this is simply adding extra initialization to onWorkbookCreate() in XSSFWorkbook.java.  I will also attach a patch showing this addition.

Thanks much!
--Leif
Comment 1 Leif Nelson 2009-06-23 09:00:10 UTC
Created attachment 23854 [details]
TestPoiDate.java - sample code exhibiting behavior
Comment 2 Leif Nelson 2009-06-23 09:06:08 UTC
Created attachment 23855 [details]
Excel 2008 without the workbookPrTag in workbook.xml (this is showing the issue)
Comment 3 Leif Nelson 2009-06-23 09:06:50 UTC
Created attachment 23856 [details]
Excel 2008 with added workbookPrTag in workbook.xml (this is showing the fix)
Comment 4 Leif Nelson 2009-06-23 09:07:51 UTC
Created attachment 23857 [details]
Excel 2007 with workbookPrTag - This is showing that Excel 2007 works the same either way.
Comment 5 Leif Nelson 2009-06-23 09:08:45 UTC
Created attachment 23858 [details]
Excel 2007 without workbookPrTag - This is showing that Excel 2007 defaults to 1900 date formatting even with the workbookPr tag missing.
Comment 6 Yegor Kozlov 2009-06-27 04:27:51 UTC
Leif,

You are absolutely correct. The date1904 flag indicates whether the date system starts in 1904. If this flag is missing then the default Excel setting is used which is different in Win Excel 2007 and Mac Excel 2008. 

The suggested fix is fine, I applied in r788956.

Regards,
Yegor
Comment 7 Leif Nelson 2009-06-29 07:53:38 UTC
Yegor,

Thanks so much!

--Leif