如何只使用行和固定数量的列进行for循环?

时间:2022-04-04 01:22:04

I have an application in Android that reads from an excel. I want to replace the while loop and put the for loop. I have only 3 columns so I think theres is another way to do this. Can you show me how to do this while loop with a for loop using only rows and 3 columns?

我在Android中有一个从excel读取的应用程序。我想替换while循环并放入for循环。我只有3列,所以我认为这是另一种方法。你能告诉我如何使用只有行和3列的for循环进行循环吗?

private void readExcelFile() {

        try{


            String inFileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"ola.xlsx";

            File file = new File(inFileName);
            Workbook workBook = WorkbookFactory.create(file);
            Sheet sheet  = workBook.getSheetAt(0);


            Iterator<Row> rowIter = sheet.rowIterator();
//this is the loop I talked about
            while(rowIter.hasNext()){


                Row myRow =rowIter.next();
                Iterator<Cell> cellIter = myRow.cellIterator();


                while(cellIter.hasNext()){
                    Cell myCell = cellIter.next();





                    Toast.makeText(getApplicationContext(), "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        }catch (Exception e){e.printStackTrace(); }

        return;
    }

3 个解决方案

#1


0  

Well it's more of algorithm than android you can loop through cells if you have row and column size:

嗯,如果你有行和列大小,它可以比android更多的算法循环遍历单元格:

int totalCount = rowSize * columnSize;
Iterator<Cell> rowIter = sheet.rowIterator();
Iterator<Cell> cellIter;

for(int i = 0 ; i < totalCount ; i++){
    if(i % rowSize == 0){
        //Move to the Next Row
        myRow = rowIter.next();
        cellIter = myRow.cellIterator();
    }

    Cell myCell = cellIter.next();
}

Though it may have a bug or two, but I think it will works like you want.

虽然它可能有一两个bug,但我认为它会像你想要的那样工作。

#2


0  

String FilePath = "E://airline.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet("Sheet2");
for(int row = 0; row < sh.getRows(); row++){
    for(int column = 0; column < sh.getColumns(); column++){
        System.out.print(sh.getCell(column, row).getContents() + "   ");
    }
}

I have done this using the JExcel library's workbook Class. Please have a look at the documentation at [blog]: http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html/"JExcel Documentation".

我使用JExcel库的工作簿类完成了这个。请查看[blog]上的文档:http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html/"Jexcel Documentation“。

The thing to note here is how getCell accepts the row and column parameters. If we pass these parameters the other way we will get Exception.

这里要注意的是getCell如何接受行和列参数。如果我们以另一种方式传递这些参数,我们将得到Exception。

#3


0  

// ... alternate looping logic using for loops
Sheet sheet  = workBook.getSheetAt(0);

for (int row = 0; row < sheet.getColumns(); row++) {
      for (int column = 0; column < sheet.getRows(); column++) {
        Cell cell = sheet.getCell(column, row);
        Toast.makeText(getApplicationContext(), "cellValue: " + cell.toString(), Toast.LENGTH_SHORT).show();
      }
 }

I looked into JExcel library, it has getRows & getColumns methods which return the no. of rows & columns respectively. Hence, I was able to iterate using for loops instead of using iterators.

我查看了JExcel库,它有getRows和getColumns方法返回no。分别为行和列。因此,我能够使用for循环而不是使用迭代器进行迭代。

#1


0  

Well it's more of algorithm than android you can loop through cells if you have row and column size:

嗯,如果你有行和列大小,它可以比android更多的算法循环遍历单元格:

int totalCount = rowSize * columnSize;
Iterator<Cell> rowIter = sheet.rowIterator();
Iterator<Cell> cellIter;

for(int i = 0 ; i < totalCount ; i++){
    if(i % rowSize == 0){
        //Move to the Next Row
        myRow = rowIter.next();
        cellIter = myRow.cellIterator();
    }

    Cell myCell = cellIter.next();
}

Though it may have a bug or two, but I think it will works like you want.

虽然它可能有一两个bug,但我认为它会像你想要的那样工作。

#2


0  

String FilePath = "E://airline.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet("Sheet2");
for(int row = 0; row < sh.getRows(); row++){
    for(int column = 0; column < sh.getColumns(); column++){
        System.out.print(sh.getCell(column, row).getContents() + "   ");
    }
}

I have done this using the JExcel library's workbook Class. Please have a look at the documentation at [blog]: http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html/"JExcel Documentation".

我使用JExcel库的工作簿类完成了这个。请查看[blog]上的文档:http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html/"Jexcel Documentation“。

The thing to note here is how getCell accepts the row and column parameters. If we pass these parameters the other way we will get Exception.

这里要注意的是getCell如何接受行和列参数。如果我们以另一种方式传递这些参数,我们将得到Exception。

#3


0  

// ... alternate looping logic using for loops
Sheet sheet  = workBook.getSheetAt(0);

for (int row = 0; row < sheet.getColumns(); row++) {
      for (int column = 0; column < sheet.getRows(); column++) {
        Cell cell = sheet.getCell(column, row);
        Toast.makeText(getApplicationContext(), "cellValue: " + cell.toString(), Toast.LENGTH_SHORT).show();
      }
 }

I looked into JExcel library, it has getRows & getColumns methods which return the no. of rows & columns respectively. Hence, I was able to iterate using for loops instead of using iterators.

我查看了JExcel库,它有getRows和getColumns方法返回no。分别为行和列。因此,我能够使用for循环而不是使用迭代器进行迭代。