简体   繁体   中英

how to check if a row is null in xlsx using java

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM