读取Excel表格的空单元格

我正在阅读Excel表格的内容使用下面的代码。

for (Row row : sheet) { Cell firstCell = row.getCell(0); Cell secondCell = row.getCell(1); Cell thirdCell = row.getCell(2); Cell fourthCell = row.getCell(3); Cell fifthCell = row.getCell(4); urlcnt=firstCell.getRichStringCellValue().getString(); srccnt=secondCell.getRichStringCellValue().getString(); contentType=thirdCell.getRichStringCellValue().getString(); verticle=fourthCell.getRichStringCellValue().getString(); timeFrame=fifthCell.getRichStringCellValue().getString(); } 

我如上所述将特定行的每个单元格分配给一个string。

 PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe, datentime) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"); insertUrlStatement.setString(1, urlcnt); insertUrlStatement.setString(2, srccnt); insertUrlStatement.setInt(3, 1); insertUrlStatement.setInt(4, 0); insertUrlStatement.setString(5, contentType); insertUrlStatement.setInt(6, 1); insertUrlStatement.setString(7, verticle); insertUrlStatement.setString(8, timeFrame); insertUrlStatement.setString(9, datentime); insertUrlStatement.executeUpdate(); 

有时在Excel工作表中,用户可能会将一个单元格留空。 在这种情况下,这个程序没有插入整行。

我想要在表urls_temp中将空单元格保存为空。

如何达到相同的目的? 请指教…

这可能是一个想法。 如果提供的variables为空,则使用一个在statment中填充null的函数。 您可能需要尝试(text = null)部分,但这可能是工作。 对不起,我可怜的编码风格。 任何数据types都需要这样的函数。

 function bindString(int column, String text) { If (text == null) then insertUrlStatement.setNull(column, STRING); Else insertUrlStatement.setString(column, text); EndIf }