如何使用Selenium在Excel文件中查找字符串的位置。

huangapple go评论122阅读模式
英文:

How to find the position of the string in excel file using selenium

问题

  1. 案例我的字符串位置是动态的所以我必须在Excel中搜索字符串位置然后获取该位置旁边的值
  2. 这是我尝试过的
  3. public class AssetRegisterDepreciationReport {
  4. public static void ReadAssetExcelData() throws IOException {
  5. String cellContent = "Grand Total:";
  6. int rownr, colnr = 5;
  7. FileInputStream assetReport = new FileInputStream("C:\\Users\\Admin\\Downloads\\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
  8. @SuppressWarnings("resource")
  9. XSSFWorkbook assetWorkbook = new XSSFWorkbook(assetReport);
  10. XSSFSheet sheetNumber1 = assetWorkbook.getSheetAt(0);
  11. rownr = findRow(sheetNumber1, cellContent);
  12. output(sheetNumber1, rownr, colnr);
  13. finish();
  14. }
  15. private static void output(XSSFSheet sheetNumber1, int rownr, int colnr) {
  16. XSSFRow row = sheetNumber1.getRow(rownr);
  17. XSSFCell cell = row.getCell(colnr);
  18. System.out.println("您的总数是:" + cell);
  19. }
  20. private static int findRow(XSSFSheet sheetNumber1, String cellContent) {
  21. int rowNum = 0;
  22. for (Row row : sheetNumber1) {
  23. for (Cell cell : row) {
  24. switch (cell.getCellType()) {
  25. case STRING:
  26. if (cell.getRichStringCellValue().getString().equals(cellContent)) {
  27. rowNum = row.getRowNum();
  28. System.out.println(rowNum);
  29. }
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. }
  36. return rowNum;
  37. }
  38. private static void finish() {
  39. System.exit(0);
  40. }
  41. }

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

  1. public class AssetRegisterdepreciationReport
  2. {
  3. public static void ReadAssetExcelData() throws IOException
  4. {
  5. String cellContent = "Grand Total:";
  6. int rownr, colnr = 5;
  7. FileInputStream AssetReport = new FileInputStream("C:\\Users\\Admin\\Downloads\\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
  8. @SuppressWarnings("resource")
  9. XSSFWorkbook Assetworkbook = new XSSFWorkbook(AssetReport);
  10. XSSFSheet sheetnumber1 = Assetworkbook.getSheetAt(0);
  11. rownr = findRow(sheetnumber1, cellContent);
  12. output(sheetnumber1, rownr, colnr);
  13. finish();
  14. }
  15. private static void output(XSSFSheet sheetnumber1, int rownr, int colnr)
  16. {
  17. XSSFRow row = sheetnumber1.getRow(rownr);
  18. XSSFCell cell = row.getCell(colnr);
  19. System.out.println("Your total is: " + cell);
  20. }
  21. private static int findRow(XSSFSheet sheetnumber1, String cellContent)
  22. {
  23. int rowNum = 0;
  24. for(Row row : sheetnumber1)
  25. {
  26. for(Cell cell : row)
  27. {
  28. switch(cell.getCellType())
  29. {
  30. case STRING:
  31. if(cell.getRichStringCellValue().getString() == cellContent)
  32. {
  33. rowNum = row.getRowNum();
  34. System.out.println(rowNum);
  35. }
  36. default:
  37. break;
  38. }
  39. }
  40. }
  41. return rowNum;
  42. }
  43. private static void finish()
  44. {
  45. System.exit(0);
  46. }
  47. }

findrow() is not returning any rownum value (default value is returned).

答案1

得分: 0

在Java中进行字符串比较时,请使用.equals()而不是==。

  1. if(cell.getRichStringCellValue().getString().equals(cellContent))
英文:

For string comparison in Java please use .equals() and not ==.

  1. if(cell.getRichStringCellValue().getString().equals(cellContent))

huangapple
  • 本文由 发表于 2020年3月17日 00:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/60710120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定