• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
使用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

posted on 2016-05-11 16:19  红无酒伤  阅读(4774)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3