Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApachePOI编写旧日期_Java_Excel_Date_Apache Poi - Fatal编程技术网

Java 使用ApachePOI编写旧日期

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

我正在使用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 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日
我的excel文件将如下所示:

|01/01/2009 | 11/11/2111 |空白单元格|

当date等于11/11/1811时,currentCell.setCellValue(date)行将值设置为-1,因此在我的excel文件中它显示为空(根据我的dateStyle)


为什么它在特定日期不起作用?我如何修复它?

找到了原因

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,唯一剩下的问题是我更改了数据