简体   繁体   English

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

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

how to get row, column coordinate in an excel file?如何获取 excel 文件中的行、列坐标? I'm trying to confirm that user name is in the file.我正在尝试确认用户名在文件中。 So i try to create a loop that read all the input in the excel file and find the same name as the user name and return the row and column.所以我尝试创建一个循环来读取 excel 文件中的所有输入并找到与用户名相同的名称并返回行和列。 This is my code.这是我的代码。

 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 ;
}

But i seems to stuck there because i don't know what to do to get the desirable output. Please help me..但我似乎被困在那里,因为我不知道该怎么做才能获得理想的 output。请帮助我..

After finding the desired name, return the cell.找到所需名称后,返回单元格。

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;
}

And then you could use it like this:然后你可以像这样使用它:

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