从Excel导入空date

我有一个excel表,从我正在读excel cel值并导入到数据库

使用Apache POI:

if (row.getCell(8) != null) { setActualDeparture(DateUtil.getJavaDate(row.getCell(8).getNumericCellValue())); } 

Excel单元格在单元格8上为空,所以它不应该导入任何东西。 但需要像1899-12-31T00:00:00这样的date

可能是什么问题 ?

Row.getCell(int cellnum)只返回NULL,如果该文件中没有任何内容存储在这个单元格中。 如果在这个单元格的文件中存储了某些内容,它将返回该单元格。 即使这只是一个NumberFormat例如或这个单元格的任何其他信息。

但是还有第二个方法Row.getCell(int cellnum, Row.MissingCellPolicy policy) http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.html#getCell%28int,%20org .apache.poi.ss.usermodel.Row.MissingCellPolicy%29可以在你的情况下使用。

例: 在这里输入图像说明

在A1中没有任何东西,但是有一个特殊的NumberFormat而不是General应用。

 import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStream; class GetEmtyCellTest { public static void main(String[] args) { try { InputStream inp = new FileInputStream("workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(0); System.out.println(row.getCell(0).getNumericCellValue()); //0.0 System.out.println(row.getCell(0, Row.RETURN_BLANK_AS_NULL)); //null FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); wb.write(fileOut); fileOut.flush(); fileOut.close(); } catch (InvalidFormatException ifex) { } catch (FileNotFoundException fnfex) { } catch (IOException ioex) { } } }