我找不到任何方法来检查POI表是否有空行。 我试过,Sheet.getLastRowNum(),Sheet.getFirstRowNum(),Sheet.rowIterator(),它们都发布了NPE。
我现在真的很困惑。
注意:但是Sheet对象本身不是null。
int numberOfSheets = wb.getNumberOfSheets();
Sheet sheet;
int maxColumnsCount = 0, headerRowIndex = 0;
Row row;
Member member;
List<Member> members = new ArrayList<>();
for (int i = 0; i < numberOfSheets; i++) {
sheet = wb.getSheetAt(i);
//find the maximum cells in a row, it is assumed that max cells container is only valid data. Otherwise Header or Footer.
//THISE LINE('sheet.getPhysicalNumberOfRows()') GIVE ME NULL POINTER EXCEPTION.
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (maxColumnsCount < row.getPhysicalNumberOfCells()) {
maxColumnsCount = row.getPhysicalNumberOfCells();
headerRowIndex = rowIndex;
} else {
break;
}
}
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (headerRowIndex < rowIndex) {
member = parseMember(row, memberExcelImportType);
member.getTownship().setId(townshipId);
if (StringUtils.isNotEmpty(member.getFullName()))
members.add(member);
}
}
}
对不起,这是堆栈跟踪。
java.lang.NullPointerException
at org.apache.poi.xssf.usermodel.XSSFSheet.getPhysicalNumberOfRows(XSSFSheet.java:842)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtilPOI.readExcelFileUsingPOI(ImportUtilPOI.java:45)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtils.readExcelFile(ImportUtils.java:32)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.doPersonImport(PersonExportImportHttpServe.java:153)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.serve(PersonExportImportHttpServe.java:86)
at com.taraaung.taraserver.server.MyServer$ValidateSession.serve(MyServer.java:2608)
at com.taraaung.taraserver.server.MyServer.serve(MyServer.java:770)
at com.taraaung.taraserver.server.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:1557)
at com.taraaung.taraserver.server.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:1152)
at java.lang.Thread.run(Thread.java:745)
嗯,刚刚发现发布NPE
的工作表是ChartSheet(即,它没有任何类型的行),然后我只检查工作表的实例并跳过图表床单。 :D if (sheet instanceof XSSFChartSheet) {
continue;
}
“谢谢你的帮助”