Apache POI: how to tell if a spreadsheet has come to an end?

0

I'm developing a personal project, and for this I'm using Java along with the Apache POI to read XLS files, how can I tell if the spreadsheet has come to an end? Thank you!

    
asked by anonymous 10.02.2018 / 04:23

1 answer

1

You can use the sheet's iterator:

        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        Workbook workbook = new XSSFWorkbook(excelFile);
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = sheet.iterator();

        while (iterator.hasNext()) {//enquanto houver linhas

            Row currentRow = iterator.next();//obtendo a linha
            Iterator<Cell> cellIterator = currentRow.iterator();//obtendo o iterator da linha (celulas da linha)

            while (cellIterator.hasNext()) {//enquanto houver celulas na linha         

              //fazendo leitura de dados da celula
            }
        }

To get the total number of rows:

int numeroUltimaLinha = sheet.getLastRowNum();//começa em 0, e se a planilha estiver vazia também retorna zero

int numeroTotalLinhas = sheet.getPhysicalNumberOfRows();//nao conta as linhas vazias

If numberNextLine and NumberTotalLines are zero, the worksheet is actually empty, there are no blank lines and no physical lines.

Code sample for reading and writing xls with Apache POI (MKyoung).

    
10.02.2018 / 18:54