我有一个应用程序在Android中读取从Excel中。我想替换while循环并放置for循环。我只有3列,所以我认为这是另一种方式来做到这一点。你可以告诉我如何做到这一点while循环使用for循环只使用行和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;
}
[如何在java中执行excel的单元迭代]可能的副本(http://stackoverflow.com/questions/16215750/how-to-do-cell-iteration-of-excel-in-java) – jmarkmurphy
This是http://stackoverflow.com/q/16215750/2296441的副本,并在此处的apache POI网站上也有答案https://poi.apache.org/spreadsheet/quick-guide.html#Iterator – jmarkmurphy
Tha does没有解决问题,因为我的Cell类没有getCell方法。我需要使用这个确切的形式。所以我需要一个不同的逻辑,使用那里存在的相同变量 – jose