我尝试使用 JExcelAPI (v. 2.6.3) 在 Java 应用程序中生成一些 Excel 工作表,但无法正确生成日期单元格。例如,对于代码:
WritableWorkbook workbook = null;
workbook = Workbook.createWorkbook(new File("C:\\tmp\\tests.xls"));
try {
Date date = new Date();
final WritableSheet sheet = workbook.createSheet("Sheet", 0);
DateTime dateTime = new DateTime(0, 0, date);
sheet.addCell(dateTime);
System.out.println("Date1 is " + date);
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2007);
cal.set(Calendar.MONTH, Calendar.OCTOBER);
cal.set(Calendar.DAY_OF_MONTH, 17);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 15);
date = cal.getTime();
dateTime = new DateTime(0, 1, date);
sheet.addCell(dateTime);
System.out.println("My birthday is on " + date);
} finally {
workbook.write();
workbook.close();
}
输出(在控制台上)是:
Date1 is Mon Jun 08 11:14:45 GMT+01:00 2009
我的生日是在 Wed Oct 17 08:15:45 GMT+01:00 2007
在 Excel 文件中,单元格是
1900-01-00 10:14:46
1900-01-00 07:15:46
Excel 中的时间部分被更正为 UTC,日期部分被丢弃。虽然参考文献提到了时区问题,但它没有提到丢弃日期。我究竟做错了什么?