首页 > 其他 > 详细

使用POI getCell 获取空的单元格之后在使用的时候报 NullPointerException

时间:2016-05-11 16:33:14      阅读:601      评论:0      收藏:0      [点我收藏+]

解决办法,在得到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

使用POI getCell 获取空的单元格之后在使用的时候报 NullPointerException

原文:http://www.cnblogs.com/huanghongbo/p/5482314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!