2015-03-13 101 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