问题遇到的现象和发生背景
有一个表格,中间用空行,按理说遇到空行已经判断了直接跳过循环, if (row == null) continue;确没有跳过,反而继续执行,进行了单元格操作。
然后 遍历单元格的时候,row.getFirstCellNum() = -1,造成异常
问题相关代码,请勿粘贴截图
//读取文件数据
public static List<List<Object>> getExcelList(InputStream is, String fileName, Integer... integers) throws Exception {
List<List<Object>> list = new ArrayList<List<Object>>();
Workbook workbook = null;//Excel
//验证文件格式
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (suffix.equals(excel2003L)) {
workbook = new HSSFWorkbook(is);
} else if (suffix.equals(excel2007U)) {
workbook = new XSSFWorkbook(is);
} else {
//throw
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
sheet = workbook.getSheetAt(0);
//遍历当前sheet中全部行
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null) continue;
//循环当前row中全部列
List<Object> li = new ArrayList<Object>();
for (int k = row.getFirstCellNum(); k <= row.getLastCellNum(); k++) {
cell = row.getCell(k);
if (cell != null) {
li.add(getCellValue(cell));
} else {
li.add("");
}
}
list.add(li);
}
return list;
}