gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:58:20 25 4
gpt4 key购买 nike

如何获取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 ;
}

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

最佳答案

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

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

关于java - 如何获取Excel中选定字符串的行、列坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60353469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com