英文:
How to find the position of the string in excel file using selenium
问题
案例:我的字符串位置是动态的,所以我必须在Excel中搜索字符串位置,然后获取该位置旁边的值。
这是我尝试过的:
public class AssetRegisterDepreciationReport {
public static void ReadAssetExcelData() throws IOException {
String cellContent = "Grand Total:";
int rownr, colnr = 5;
FileInputStream assetReport = new FileInputStream("C:\\Users\\Admin\\Downloads\\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook assetWorkbook = new XSSFWorkbook(assetReport);
XSSFSheet sheetNumber1 = assetWorkbook.getSheetAt(0);
rownr = findRow(sheetNumber1, cellContent);
output(sheetNumber1, rownr, colnr);
finish();
}
private static void output(XSSFSheet sheetNumber1, int rownr, int colnr) {
XSSFRow row = sheetNumber1.getRow(rownr);
XSSFCell cell = row.getCell(colnr);
System.out.println("您的总数是:" + cell);
}
private static int findRow(XSSFSheet sheetNumber1, String cellContent) {
int rowNum = 0;
for (Row row : sheetNumber1) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
if (cell.getRichStringCellValue().getString().equals(cellContent)) {
rowNum = row.getRowNum();
System.out.println(rowNum);
}
break;
default:
break;
}
}
}
return rowNum;
}
private static void finish() {
System.exit(0);
}
}
findRow()
没有返回任何行号值(默认值被返回)。
英文:
case: my string position is dynamic so i have to search the string position in the excel and then fetch the value next to that position.
This is what i have tried
public class AssetRegisterdepreciationReport
{
public static void ReadAssetExcelData() throws IOException
{
String cellContent = "Grand Total:";
int rownr, colnr = 5;
FileInputStream AssetReport = new FileInputStream("C:\\Users\\Admin\\Downloads\\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook Assetworkbook = new XSSFWorkbook(AssetReport);
XSSFSheet sheetnumber1 = Assetworkbook.getSheetAt(0);
rownr = findRow(sheetnumber1, cellContent);
output(sheetnumber1, rownr, colnr);
finish();
}
private static void output(XSSFSheet sheetnumber1, int rownr, int colnr)
{
XSSFRow row = sheetnumber1.getRow(rownr);
XSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(XSSFSheet sheetnumber1, String cellContent)
{
int rowNum = 0;
for(Row row : sheetnumber1)
{
for(Cell cell : row)
{
switch(cell.getCellType())
{
case STRING:
if(cell.getRichStringCellValue().getString() == cellContent)
{
rowNum = row.getRowNum();
System.out.println(rowNum);
}
default:
break;
}
}
}
return rowNum;
}
private static void finish()
{
System.exit(0);
}
}
findrow() is not returning any rownum value (default value is returned).
答案1
得分: 0
在Java中进行字符串比较时,请使用.equals()而不是==。
if(cell.getRichStringCellValue().getString().equals(cellContent))
英文:
For string comparison in Java please use .equals() and not ==.
if(cell.getRichStringCellValue().getString().equals(cellContent))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论