2010-02-03 77 views

回答

17

我想你會必須遍歷行並檢查每個行上的HSSFRow.getLastCellNum()

+1

對於XLSX文件,你必須改變的唯一事情是HSSF到XSSF。 XSSFRow.getLastCellNum() – 2014-11-19 12:20:26

3

檢查每一行並致電Row.getLastCellNum()最大單元號是最後一個列號。

Row r = sheet.getRow(rowNum); 
int maxCell= r.getLastCellNum(); 
0

結識有任何行的值,最後一列,首先,你需要獲得行,然後你可以找到具有價值

語法的最後一列:
sheet.getrow (ROWNUMBER).getLastCellNum();

ROWNUMBER - >是您要知道,擁有價值

0

最後一列試試這個功能的行號:

private void maxExcelrowcol() { 
    int row, col, maxrow, maxcol; 

    //Put file name here for example filename.xls 
    String filename = "filename.xls"; 
    static String TAG = "ExelLog"; 

    //you can use 'this' in place of context if you want 
    Context context = getApplicationContext(); 

    try { 
     // Creating Input Stream 
     File file = new File(context.getExternalFilesDir(null), filename); 
     FileInputStream myInput = new FileInputStream(file); 

     // Create a POIFSFileSystem object 
     POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 

     // Create a workbook using the File System 
     HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); 

     // Get the first sheet from workbook 
     HSSFSheet mySheet = myWorkBook.getSheetAt(0); 

     //Row iterator 
     Iterator rowIter = mySheet.rowIterator(); 

     while (rowIter.hasNext()) { 
      HSSFRow myRow = (HSSFRow) rowIter.next(); 
      //Cell iterator for iterating from cell to next cell of a row 
      Iterator cellIter = myRow.cellIterator(); 
      while (cellIter.hasNext()) { 
       HSSFCell myCell = (HSSFCell) cellIter.next(); 

       row = myCell.getRowIndex(); 
       col = myCell.getColumnIndex(); 

       if (maxrow < row) { 
        maxrow = row; 
       } 
       if (maxcol < col) { 
        maxcol = col; 
       } 
      } 
     } 
    } catch(FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

感謝您的回答。如果您還想在代碼中添加一些註釋,那將會很棒。這有助於人們更好地理解你的答案。 – hofmeister 2018-03-06 13:26:21