Java 使用ApachePOI编写旧日期
我正在使用ApachePOI3.14创建一个excel文件,该文件必须包含不同类型的数据,包括日期 我的代码在大多数情况下都能正常工作。 在单元格中编写特定日期(java.util.Date)时,我遇到了一些问题。 这是我的密码:Java 使用ApachePOI编写旧日期,java,excel,date,apache-poi,Java,Excel,Date,Apache Poi,我正在使用ApachePOI3.14创建一个excel文件,该文件必须包含不同类型的数据,包括日期 我的代码在大多数情况下都能正常工作。 在单元格中编写特定日期(java.util.Date)时,我遇到了一些问题。 这是我的密码: Date date; // code where I get the value of date Cell currentCell = currentRow.createCell(fieldPos++); // line below doesn't wo
Date date;
// code where I get the value of date
Cell currentCell = currentRow.createCell(fieldPos++);
// line below doesn't work for date like '11/11/1811' but work for '11/11/2111'
currentCell.setCellValue(date);
currentCell.setCellStyle(myDateStyle);
假设我有3个日期要写:
- 01/01/2009
- 2111年11月11日
- 1811年11月11日
为什么它在特定日期不起作用?我如何修复它?找到了原因 Apache Poi的setCellValue(日期d)尝试将给定的java.util.Date转换为excel“datenumber”: Excel将日期和时间存储为一个数字,表示自1900年1月0日起的天数,加上24小时工作日的小数部分:ddddd.tttttt。这称为序列日期或序列日期时间 这就是为什么当给定1900年之前的日期时,setCellValue会将该值设置为-1,因为它无法进行转换操作 修复了我用Date.toString()调用setCellValue(字符串s)的问题。如果setCellValue(日期d)集为-1,唯一剩下的问题是我更改了数据