2017-04-18 40 views
1

我有一个读取excel并获取信息的应用程序。这是我的代码,从列0和列1读取信息,我想知道的方式从33如何在使用apache poi的循环中的特定单元格上启动?

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


      Iterator<Row> rowIter = sheet.rowIterator(); 
      while(rowIter.hasNext()){ 

       Row myRow =rowIter.next(); 


       Cell cell1 = myRow.getCell(0); 
       Cell cell2 = myRow.getCell(1); 
       ... 


       Iterator<Cell> cellIter = myRow.cellIterator(); 
       while(cellIter.hasNext()){ 
        Cell myCell = cellIter.next(); 

       } 

       //bd.addAnimalls(cell1.toString(),cell2.toString()); 

       Toast.makeText(getApplicationContext(), "on inserting", Toast.LENGTH_SHORT).show(); 



      } 

回答

1

就行号34才开始,并跳过第1行可以使用sheet.getRow(rowNum)获得特定行。

所以,你的代码可以改成这样:

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

int rowCtr = 33; 
Row myRow = sheet.getRow(rowCtr++); 
while (myRow != null) { 
    Cell cell1 = myRow.getCell(0); 
    // rest of your loop code 

    myRow = sheet.getRow(rowCtr++); 
} 
+0

我把里面虽然(rowIter.hasNext)? – jose

+0

这是要取代你的循环。我会编辑给你一个更好的主意。 – msitt

+0

你可以编辑我的原始代码吗? – jose

相关问题