2010-02-03 129 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