Row titleRow = sheet.getRow(0);//标题行
for(int i=1;i<rowCount;i++){//遍历行,略过标题行,从第二行开始
Row row = sheet.getRow(i);
//跳过空行
if(i>=1) {
if (row == null) {
continue;
} else if (row.getCell(0) == null ||
StringUtils.isNullOrEmpty(row.getCell(0).getStringCellValue())) {
continue;
}
}
TThesis thesis = new TThesis();
//作者
if(titleRow.getCell(0).getStringCellValue().indexOf("作者")>=0){
if(row.getCell(0)!=null && row.getCell(0).getCellType()==row.getCell(0).CELL_TYPE_STRING){
thesis.setAuthor(row.getCell(0).getStringCellValue().trim());
}
}
//题名
if(titleRow.getCell(1).getStringCellValue().indexOf("题名")>=0){
if(row.getCell(1)!=null && row.getCell(0).getCellType()==row.getCell(1).CELL_TYPE_STRING){
thesis.setTitle(row.getCell(1).getStringCellValue().trim());
}
}
}
如上面的代码所示,跳过空行就不会出现空指针异常了。