繁体   English   中英

如何获取 excel 中选定字符串的行、列坐标

[英]How do i get row,column coordinates on selected String in excel

如何获取 excel 文件中的行、列坐标? 我正在尝试确认用户名在文件中。 所以我尝试创建一个循环来读取 excel 文件中的所有输入并找到与用户名相同的名称并返回行和列。 这是我的代码。

 private int ReadName(String Name){

    Iterator<Row> rowIterator = librarianSheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            if(cell.getCellType() == CellType.STRING){
                String random = cell.getRichStringCellValue().getString();
            }
            if(random == Name){

            }
        }
    }
    //return ;
}

但我似乎被困在那里,因为我不知道该怎么做才能获得理想的 output。请帮助我..

找到所需名称后,返回单元格。

private Cell readName(String name){
    Iterator<Row> rowIterator = librarianSheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            if(cell.getCellType() == CellType.STRING){
                String random = cell.getStringCellValue();
                if(random.equals(name)){
                    return cell;
                }
            }
        }
    }
    return null;
}

然后你可以像这样使用它:

Cell cell = readName("Muhammad");
if(cell != null) {
    int column = cell.getColumnIndex();
    int row = cell.getRowIndex();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM