使用POI getCell 獲取空的單元格之后在使用的時候報 NullPointerException


解決辦法,在得到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?

Iterate over cells, with control of missing / blank cells


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM