Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过提供列位置POI来读取选定列的函数_Java_Apache Poi - Fatal编程技术网

Java 通过提供列位置POI来读取选定列的函数

Java 通过提供列位置POI来读取选定列的函数,java,apache-poi,Java,Apache Poi,excel文件中的示例数据,这里的第一行是标题 Demo1 Demo2 Demo3 Demo4 Demo5 Demo6 DummyText1 DummyText2 DummyText3 DummyText4 DummyText5 DummyText6 DummyText11 DummyText21 DummyText31 D

excel文件中的示例数据,这里的第一行是标题

Demo1           Demo2           Demo3           Demo4           Demo5           Demo6
DummyText1      DummyText2      DummyText3      DummyText4      DummyText5      DummyText6
DummyText11     DummyText21     DummyText31     DummyText41     DummyText51     DummyText61
下面包含选择性列的代码工作正常,但对于另一列,我必须再次在if条件中添加cell1.getColumnIndex,以此类推:

所以我创建了下面的函数来skipCells;然而,它并没有像预期的那样工作

for (Cell cell : row) {
    skipCells(cell,1,3,6) // this should read only column 1,3 and 6, but it is not working.
    String result = getCellData(xssfWorkbook, cell);
    System.out.println(result);
}

static void skipCells(Cell cell,int ...cellPosition)
{
    for (int i: cellPosition) {
        if (cell.getColumnIndex() != i) {
            continue;
        }
    }
}

你只是在单元格上迭代,什么都不做,把skipCells改为

static void skipCells(Cell cell,int ...cellPosition) {
    for (int i: cellPosition) {
        if (cell.getColumnIndex() == i) {
            break;
        }
    }
}
或者只是创建一个要读取的列的列表,并检查该列是否在列表中

List<Integer> list = Arrays.asList(1, 3, 6);
for (Cell cell : row) {
    if (list.contains(cell.getColumnIndex())) {
        String result = getCellData(xssfWorkbook, cell);
        System.out.println(result);
    }
}

将skipCells函数的返回类型更改为Cell

static Cell skipCells(Cell cell, int... cellPosition){
    Cell c = null;
    for (int i: cellPosition) {
        if (cell.getColumnIndex() != i) {
            continue;
        }
        c = cell;
    }
    return c;
}

for (Cell cell : row) {
    Cell readCell = skipCells(cell, 1, 3, 6)
     if(readCell != null ) { // remove null value if any
        System.out.println(getCellData(xssfWorkbook, readCell));               
    }
}

感谢@Prasad的回复,如果有超过6列,我必须再次添加案例。这是我不想要的。将HSSFRow传递到某个函数,并尝试一些你将实现的逻辑钩/钩,因为休息只是头脑风暴,你正在向右转。第二个条件非常有效。非常感谢你。
    while (rows.hasNext())
    {
    int colNumber = 0;
        row=(HSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        while (cells.hasNext())
        {colNumber++;
            cell=(HSSFCell) cells.next();

            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
            {
                System.out.print(cell.getStringCellValue()+" ");
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
            {
                System.out.print(cell.getNumericCellValue()+" ");
            }
            else
            {
                //U Can Handel Boolean, Formula, Errors
            }



             switch(colNumber){
                                        case 1:
                                            singleRecord.setXX1(cellValue);
                                            break;
                                        case 2:
                                            singleRecord.setXX2(cellValue);
                                            break;
                                        case 3:
                                            singleRecord.setXX3(cellValue);
                                            break;
                                        case 4:
                                            singleRecord.setXX4(cellValue);
                                            break;
                                        case 5:
                                            singleRecord.setXX5(cellValue);
                                            break;
                                        case 6:
                                            singleRecord.setXX6(cellValue);
                                            break;
                                        default:
                                            System.out.println("--> Invalid column");
                            }   





        }
        System.out.println();
    }
     /////////////////////////////////////
     create bean class and add getter/setter properties inside switch case 
     you can later add this object to LinkList also and process as you wish
static Cell skipCells(Cell cell, int... cellPosition){
    Cell c = null;
    for (int i: cellPosition) {
        if (cell.getColumnIndex() != i) {
            continue;
        }
        c = cell;
    }
    return c;
}

for (Cell cell : row) {
    Cell readCell = skipCells(cell, 1, 3, 6)
     if(readCell != null ) { // remove null value if any
        System.out.println(getCellData(xssfWorkbook, readCell));               
    }
}