我正在尝试以这样一种方式解析 excel 文件,即当我找到一个空单元格时,我将添加到字符串的值中。示例代码:
for (int e = 0; e < wb.getNumberOfSheets(); e++) {
sheet = wb.getSheetAt(e);
for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {
System.out.println(sheet.getSheetName());
row = sheet.getRow(i);
if(row != null) {
cell = row.getCell(0, Row.RETURN_BLANK_AS_NULL);
if(cell.getCellType() != Cell.CELL_TYPE_BLANK) {
cell = row.createCell(i);
cell.setCellValue("foo");
}
cell = row.getCell(1, Row.RETURN_BLANK_AS_NULL);
if(cell == null) {
cell = row.createCell(i);
cell.setCellValue("bar");
}
cell = row.getCell(2, Row.RETURN_BLANK_AS_NULL);
if(cell == null) {
cell = row.createCell(i);
cell.setCellValue(0.0);
}
}
}
我尝试了很多解决方案,我总是得到一个异常 NullPointerException。