【问题标题】:How to increment my row counter variable in my while loop?如何在我的while循环中增加我的行计数器变量?
【发布时间】:2017-07-15 22:29:56
【问题描述】:

我正在尝试将第一个单元格中包含数据的每一行中的数据读入对象的 ArrayList。我的问题是我的代码似乎没有增加我的计数器超过第一行。我错过了一些简单的东西吗?

代码

        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++;
            }   
    }

输出

[ValuSmart Series 1120 Double Hung]

... 我得到这个输出 72 次,因为工作表中有 72 行

隔离循环

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());

【问题讨论】:

  • 使用调试器并逐行执行代码将帮助您发现问题,并为您节省未来无数小时的调试时间。为什么不现在就开始做呢?
  • 你的迭代器的目的是什么?
  • int rowIndex = 0; XSSFRow 行 = sheet.getRow(rowIndex);从技术上讲,您的“行”对象已经在循环外初始化,您只是在循环内使用相同的对象。
  • 我的迭代器遍历行中的每个单元格,如果单元格不为空,则将其添加到 ArrayList 对象。 @shmosel
  • @YohannesGebremariam 你是在告诉我把这两行代码放在循环中吗?

标签: java while-loop apache-poi increment


【解决方案1】:

您不会在任何地方选择下一行,而且您的循环会令人困惑,并且会在基于索引和基于迭代器的查找之间切换。尝试一个简单的增强 for 循环:

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

【讨论】:

  • 最后一件事。一旦单元格为空,我就不想再阅读该行了。我正在努力弄清楚@shmosel 的逻辑
  • @KyleHarbour 将if 更改为if (cell == null) { break; } 并移出下一行。
  • 使用增强循环,你只会得到存在的单元格,所以你不会得到一个空单元格。打破空细胞的目的是什么?您的电子表格中的行中间是否缺少单元格?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
相关资源
最近更新 更多