Java 从Excel导入空日期
我有一张excel表,从中读取excel cel值并导入数据库 使用Apache POI:Java 从Excel导入空日期,java,excel,apache-poi,Java,Excel,Apache Poi,我有一张excel表,从中读取excel cel值并导入数据库 使用Apache POI: if (row.getCell(8) != null) { setActualDeparture(DateUtil.getJavaDate(row.getCell(8).getNumericCellValue())); } excel工作表的第8单元格为空,因此不应导入任何内容。但它需要像1899-12-31T00:00:00这样的日期 可能是什么问题 Row.getCell
if (row.getCell(8) != null) {
setActualDeparture(DateUtil.getJavaDate(row.getCell(8).getNumericCellValue()));
}
excel工作表的第8单元格为空,因此不应导入任何内容。但它需要像1899-12-31T00:00:00这样的日期
可能是什么问题 Row.getCell(int-cellnum)如果文件中没有为该单元格存储任何内容,则仅返回NULL。如果该单元格的文件中存储了某些内容,则返回该单元格。即使这只是一个数字格式,例如,或此单元格的任何其他信息
但是还有第二种方法Row.getCell(int-cellnum,Row.MissingCellPolicy)
可以在您的案例中使用
例如:
A1中没有任何内容,但有一种特殊的数字格式,不适用于一般情况
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) {
}
}
}
第8单元是该行的第9个单元。这会改变什么吗?是的,这不会改变任何东西。请注意:此excel工作表是使用java脚本创建的。如果单元格(8)为空,则通过getNumericCellValue()调用将获得NullPointerException。显然,它不是空的。打开excel电子表格。你看到里面有什么数据吗?它是打开的,是空的(空白)(空白)