有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从Excel导入空日期

我有一张excel表,从中读取excel cel值并导入数据库

使用Apache POI:

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

excel工作表的第8单元格为空,因此不应导入任何内容。但它需要像1899-12-31T00:00:00这样的日期

可能是什么问题


共 (1) 个答案

  1. # 1 楼答案

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

    但是还有第二种方法Row.getCell(int cellnum, Row.MissingCellPolicy policy){a1},可以在您的案例中使用

    例如: enter image description here

    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) {
      }
     }
    }