Java POI。空白时跳过单元格

时间:2017-02-04 17:00:23

标签: java excel javafx apache-poi

我收到错误:“无法从STRING单元格中获取NUMERIC值”,我确定这是因为单元格为空。我正在从xlsx读取数据并将其写入我的数据库。有没有人有一个简单的方法来跳过空白单元格并继续进行?

       FileInputStream fileIn = new FileInputStream(filename);
      Workbook wb = new XSSFWorkbook(fileIn);
        Sheet sheet = wb.getSheetAt(0);

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


        Row row; 
      for  (int i=1; i<=sheet.getLastRowNum(); i++){
            row = sheet.getRow(i);


            pst.setObject(1, row.getCell(0).getNumericCellValue());
            pst.setString(2, row.getCell(1).getStringCellValue());
            pst.setObject(3, sdf.format(row.getCell(2).getDateCellValue()));
            pst.setObject(4, sdf.format(row.getCell(3).getDateCellValue()));
            pst.setString(5, row.getCell(4).getStringCellValue());
            pst.setObject(6, sdf.format(row.getCell(5).getDateCellValue()));
            pst.setDouble(1, row.getCell(0).getNumericCellValue());
            pst.execute();
                        }


        wb.close();
        fileIn.close();
        pst.close();
        rs.close();
loadDataFromDatabase();
}catch (SQLException  ex){
            Logger.getLogger(TestTableController.class.getName()).log(Level.SEVERE,null,ex);
        }catch (IOException ex){
            Logger.getLogger(TestTableController.class.getName()).log(Level.SEVERE,null,ex);
        }
}

1 个答案:

答案 0 :(得分:0)

感谢来自Youtube的Ram Alapure的帮助。 当我尝试从Excel导入数据时要记住两件事。 1.突出显示所有空单元格和清晰内容。在此之前,我会得到 错误无法从STRING单元格获取NUMERIC值,如果我从getStringCellValue()更改为getDateCellValue(),我将收到错误无法从NUMERIC单元格获取STRING值。它就像薛定谔的猫一样

  1. 对NullPointerException使用try catch。
  2. 以下是我的工作代码。

    FileInputStream fileIn = new FileInputStream(filename);
          Workbook wb = new XSSFWorkbook(fileIn);
            Sheet sheet = wb.getSheetAt(0);
    
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    
    
    
            Row row; 
    
    
      for (int i=1; i<=sheet.getLastRowNum(); i++){
                row = sheet.getRow(i);
    
                System.out.println(row.getCell(5)); 
                try{
    
                        if(row.getCell(5).getDateCellValue() ==  null){
    Date date = new Date(); // set any default value
    pst.setObject(6, null); // or you can set it to null if null is allowed   i.e. pst.setObject(6, null);
     }else{
    pst.setObject(6, sdf.format(row.getCell(5).getDateCellValue()));
                        }
    
    
                pst.setObject(1, row.getCell(0).getNumericCellValue());
                pst.setString(2, row.getCell(1).getStringCellValue());
                pst.setObject(3, sdf.format(row.getCell(2).getDateCellValue()));
                pst.setObject(4, sdf.format(row.getCell(3).getDateCellValue()));
                pst.setString(5, row.getCell(4).getStringCellValue());
                pst.setObject(6, sdf.format(row.getCell(5).getDateCellValue()));
                pst.setDouble(7, row.getCell(6).getNumericCellValue());
    
                }catch(NullPointerException NPE)
    
              {
    
                 }
    
                pst.execute();
    
        }
    
    
            wb.close();
            fileIn.close();
            pst.close();
            rs.close();
    loadDataFromDatabase();
    }catch (SQLException  ex){
                Logger.getLogger(TestTableController.class.getName()).log(Level.SEVERE,null,ex);
            }catch (IOException ex){
                Logger.getLogger(TestTableController.class.getName()).log(Level.SEVERE,null,ex);
            }
                }
    }