解決辦法,在得到cell之后先判斷cell是否為空,然后再進行邏輯處理。
得到的cell建議使用去除策略(如左對齊,居中等)的cell,不然有可能受到策略影響而導致結果異常。
org.apache.poi.ss.usermodel.Row.MissingCellPolicy policy = org.apache.poi.ss.usermodel.Row.RETURN_NULL_AND_BLANK; for (int cn = 0; cn < lastColumn; cn++) { Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL); if (c == null) { // The spreadsheet is empty in this cell } else { // Do something useful with the cell's contents } }
參考資料:
How to avoid NullPointerException with an empty cell using POI?