2010-11-17 187 views
1

我正在阅读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(); 
} 

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

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中将空单元格保存为空。

如何达到相同?请告知...

+0

表urls_temp是否有一个约束条件,要求url非空或长度大于零? – 2011-04-15 17:02:35

回答

0

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

function bindString(int column, String text) { 

    If (text == null) then 
    insertUrlStatement.setNull(column, STRING); 
    Else 
    insertUrlStatement.setString(column, text); 
    EndIf 

}