我正在使用 Apache POI 读取 xlsx 文件,效果很好。我有个问题要问你,当发现行为空时,我该如何处理它?我的文件仅包含 50 行,但它显示 1000 行,其余行发现为空。
try {
FileInputStream file = new FileInputStream(new File("D://productData.xlsx"));
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
CountryCode countryCode = userDao.getCode(sheet.getRow(1).getCell(4).toString());
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
continue;
} else if (row.getRowNum() == 1) {
//some operation
} else if (row.getRowNum() == 2) {
continue;
} else {
ExcelData excelData = new ExcelData();
try {
for (int i = 0; i <= 6; i++) {
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
}
//some operation
} catch (Exception e) {
e.printStackTrace();
}
}
}
file.close();
response.setResponse(data);
} catch (Exception e) {
e.printStackTrace();
}
how can i read null row from excel and how can terminate from iterator when row is null in the excel sheet. Thanks
最佳答案
如果您获取一行,并返回 null,则意味着文件中没有存储该行的数据 - 它完全是空白的。
默认情况下,POI 会向您提供文件中的内容。使用单元格,您可以设置 MissingCellPolicy 来控制如何处理丢失和空白单元格。 Apache POI 文档中有一些使用此功能的示例。对于行,它们要么存在,要么不存在,因此您需要在获取行时检查空值。
关于java - 如何使用java检查xlsx中的行是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269219/