使用POI读取excel文件

时间:2015-03-13 07:24:18

标签: apache-poi

从excel文件中读取数据时,我们可以使用

no_of_rows = sheet.getPhysicalNumberOfRows();

获取包含数据的行数。有没有办法在表格的第一列中获得包含数据的行数?

2 个答案:

答案 0 :(得分:1)

没有任何方法可以在apache poi中获取特定列的行数。你手动需要逐个遍历行,然后就可以得到它。

要获取特定列的行数,您可以refer to this link

答案 1 :(得分: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);
            }
相关问题