2015-03-13 97 views
0

雖然從Excel文件中讀取數據,我們可以使用閱讀excel文件採用POI

no_of_rows = sheet.getPhysicalNumberOfRows();

得到有數據的行數。有沒有辦法獲得表格第一列中有數據的行數?

回答

0

每列都有不同的行數?

int rows; //行號 rows = sheet.getPhysicalNumberOfRows();

int cols = 0; // No of columns 
int tmp = 0; 
for (int i = 0; i < rows; i++) { 
    row = sheet.getRow(i); 
    if (row != null) { 
     tmp = sheet.getRow(i).getPhysicalNumberOfCells(); 
     if (tmp > cols) { 
      cols = tmp; 
     } 
    } 
} 

if (cols == 23) { 
    row = sheet.getRow(0); 
    if ("I.D.Number".equals(row.getCell(0).toString())) { 
     if ("Name".equals(row.getCell(1).toString())) { 
      if ("Surname".equals(row.getCell(2).toString())) { 
              applicaple = true; 
                          } 
                         } 

       } 

} 


Iterator rowsExcel = sheet.rowIterator(); 

XSSFRow row2 = null; 
if (uygunluk == true) { 
    while (rowsExcel.hasNext()) { 

     row2 = (XSSFRow) rowsExcel.next(); 
     if (row2.getRowNum() != 0) { 
      if (fmt.formatCellValue(row2.getCell(0)) == null || (fmt.formatCellValue(row2.getCell(0)).trim().equalsIgnoreCase(""))) { 
       System.out.print.ln("warn!!"); 
       return; 
      } else if (fmt.formatCellValue(row2.getCell(0)) != null || !(fmt.formatCellValue(row2.getCell(0)).trim().equalsIgnoreCase(""))) { 
       idInfo = fmt.formatCellValue(row2.getCell(0); 
      } 
1

沒有任何方法可以獲取apache poi中特定列的行數。您需要手動逐個遍歷行,然後才能獲取它。

以獲得特定列的行數您可以refer to this link