简体   繁体   English

如何在while循环中增加行计数器变量?

[英]How to increment my row counter variable in my while loop?

I am trying to read in each row that has data in the first cell into an ArrayList of Objects. 我试图将第一个单元格中具有数据的每一行读取到对象ArrayList中。 My problem is that my code doesn't seem to be incrementing my counter past the first row. 我的问题是我的代码似乎没有使我的计数器超过第一行。 Am I missing something simple? 我是否缺少简单的东西?

Code

        try
            {
                wb = new XSSFWorkbook(new FileInputStream(fileName));
            } 
        catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } 
        catch (IOException e)
            {   
                e.printStackTrace();
            }

        XSSFSheet sheet = wb.getSheetAt(2);
        ArrayList<Object> obj = new ArrayList<Object>();
        int rowIndex = 0;
        int cellIndex = 0;
        XSSFRow row = sheet.getRow(rowIndex);
        Iterator<Cell> rowItr = row.iterator();

        while(rowIndex <= sheet.getLastRowNum())
            {
                if(row.getCell(0) == null)
                    {
                        continue;
                    }
                else
                    {
                        while(rowItr.hasNext() && rowItr.next() != null)
                                    {
                                        XSSFCell cell = row.getCell(cellIndex);

                                        if(cell == null)
                                            {
                                                continue;
                                            }
                                        else
                                            {       
                                                obj.add(row.getCell(cellIndex).toString());
                                            }
                                        cellIndex++;
                                    }

                                rowIndex++;
                                cellIndex = 0;

                            }

                        System.out.println(obj.toString());
                    }
                rowIndex++;
            }   
    }

Output 输出量

[ValuSmart Series 1120 Double Hung]

... I get this output 72 times since there are 72 rows in the sheet ... 我得到此输出72次,因为工作表中有72行

Isolated Loop 隔离回路

ArrayList<Object> obj = new ArrayList<Object>();
int rowCounter = 16;
        int x = 0;

        while(rowCounter <= 21)
            {

                XSSFRow row = sheet.getRow(rowCounter);
                Iterator<Cell> rowItr = row.iterator();

                while(rowItr.hasNext() && rowItr.next() != null)
                    {

                                XSSFCell cell = row.getCell(x);


                                if(cell == null)
                                    {
                                        continue;
                                    }
                                else
                                    {

                                        obj.add(row.getCell(x).toString());
                                    }
                        x++;

                    }

                rowCounter++;
                x = 0;
            }
        System.out.println(obj.toString());

You're not select the next row anywhere, and your loops are confusing and switch between index- and iterator-based lookups. 您不会在任何地方选择下一行,并且循环很混乱,并且在基于索引和基于迭代器的查找之间进行切换。 Try a simple enhanced for loop: 尝试一个简单的增强型for循环:

for (Row row : sheet) {
    for (Cell cell : row) {
        if (cell != null) {
            obj.add(row.getCell(x).toString());
        }
    }
}
System.out.println(obj.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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