如何搜索特定行以在 excel 文件中查找特定字符串(Apache POi)

How to search a specific row to find a specific string in excell files(Apache POi)

我正在编写一个程序,当我给程序一个字符串名称时,我想搜索 excel 文件和特定行中为字符串指定的特定行...例如,我有一个 excel 文件即:

克里斯·巴甫洛夫的名字安东·尼克·约翰

我希望当字符串变量名称为 John 时查找 excel 文件中 "John" 所在的单元格值。所以我搜索了整个第一行,找到了名称 "John".

的单元格值

我还没有尝试过任何东西,因为我不知道该怎么做。

import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;

// 最初你必须像这样创建一个工作簿..

String FilePath = " "; // input excel file location.
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);

访问 sheet

Sheet sh = wb.getSheet("Sheet1");

获取 sheet

中存在的行数
int totalNoOfRows = sh.getRows();

获取sheet

中的列数
int totalNoOfCols = sh.getColumns();

然后遍历 excel sheet 以查找特定数据..

for (int row = 0; row < totalNoOfRows; row++) {

            for (int col = 0; col < totalNoOfCols; col++) {
                //your comparision code goes here
                // if(sh.getCell(col, row).getContents().equals(yourString) )  

            }

希望这对您有所帮助:)..