在我的程序中,我想在excel表中找到与我作为参数传递的字符串匹配的行号 . 它适用于第一行和第二行但问题是下一行 . 我找到行号的代码如下:
public int findrownum(String sName, String value, int cNum) throws Exception{
File excel = new File(filepath);
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet(sName);
boolean check = true;
int i=0;
while (check){
XSSFRow rowH = ws.getRow(i);
XSSFCell cell = rowH.getCell(cNum);
String cellvalue = cellToString(cell);
if (cellvalue.equals(value)){
check = false;
}
else {
i = i+1;
}
}
return i;
}
}
我想从excel读取第三行,即具有名称注册的字符串
Sl没有测试用例名称结果时间戳1登录通过03/03/2014 12:11:43 PM 2注册
请告诉我代码中需要进行哪些更改 .
谢谢
我在JUNIT中使用了@eric提到的类似逻辑,现在我能够找到行号 . 但是当我尝试使用此行号读取数据时,它给出了错误 . 我的数据读取代码如下 . 请告诉我需要做什么更改public String dataread(String sName,int rNum,String cName)throws Exception {File excel = new File(filepath); FileInputStream fis = new FileInputStream(excel); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet ws = wb.getSheet(sName); XSSFRow rowH = ws.getRow(rNum-1); int totalRows = ws.getLastRowNum();
int i =0;
for(i=0;i<=totalRows;i++)
{
XSSFCell cell = rowH.getCell(i);
String value = cellToString(cell);
if (value.equals(cName)){
System.out.println(i);
break;
}
}
XSSFRow row = ws.getRow(rNum);
XSSFCell cell = row.getCell(i);
String value = cellToString(cell) return value;
}
2 回答
通常From this Documentation你可以使用getHeight()来获取光标,而不是编写自己的循环 . 显然这也会减少执行时间 . 您编写的代码也可能导致异常,因为没有更多的物理行 .
希望以下代码有所帮助 . 假设单元格中的数据是字符串数据 . 这也是apache poi api .