简体   繁体   English

读取Excel工作表的空单元格

[英]Reading Empty Cell of Excel Sheet

I am reading contents of the excel sheet using the following code. 我正在使用以下代码读取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();
}

I am assigning each cell of a particular row to a string as above. 我如上所述将特定行的每个单元格分配给一个字符串。

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();

Sometimes in the excel sheet, a cell may be left empty in a row by the user. 有时,在Excel工作表中,用户可能会连续将单元格留空。 During such cases this program is not inserting that whole row. 在这种情况下,该程序不会插入整个行。

I want the empty cell to be saved as null in the table urls_temp. 我希望将空单元格保存为表urls_temp中的null。

How to attain the same? 如何达到相同? Please advise... 请指教...

this might be an idea. 这可能是个主意。 Use a function which fills null in the statment if the supplied variable is empty. 如果提供的变量为空,则使用在语句中填充null的函数。 You might need to experiment with the (text = null) part but this could to the job. 您可能需要尝试使用(text = null)部分,但这可以完成工作。 Sorry for my poor coding style. 对不起,我的编码风格不佳。 You will need such a function for any datatype. 对于任何数据类型,您都将需要这样的函数。

function bindString(int column, String text) {

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM