Reputation: 49
I am using Apache POI to read xlsx file, it works well. I have question to you when row is found null, how I'm able to handle it? My file only contain 50 row, but it show 1000 row, rest of row found null.
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
Upvotes: 0
Views: 4455
Reputation: 3507
If you fetch a row, and get back null, then that means there is no data stored in the file for that row - it's completely blank.
POI by default gives you what's in the file. With Cells, you can set a MissingCellPolicy to control how missing and blank cells are handled. There's some examples of using this in the Apache POI docs. With rows, they're either there or not, so you need to check for nulls when fetching a row.
Upvotes: 2