这个excel方法在做什么?
我在这里有一个代码片段:
public class ExcelUtils { private static XSSFSheet ExcelWSheet; private static XSSFWorkbook ExcelWBook; private static XSSFCell Cell; public static void setExcelFile(String Path) throws Exception { FileInputStream ExcelFile = new FileInputStream(Path); ExcelWBook = new XSSFWorkbook(ExcelFile); } public static String getCellData(int RowNum, int ColNum, String SheetName ) throws Exception{ ExcelWSheet = ExcelWBook.getSheet(SheetName); try{ Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); String CellData = Cell.getStringCellValue(); return CellData; }catch (Exception e){ return""; } } public static int getRowCount(String SheetName){ ExcelWSheet = ExcelWBook.getSheet(SheetName); int number=ExcelWSheet.getLastRowNum()+1; return number; } public static int getRowContains(String sTestCaseName, int colNum,String SheetName) throws Exception{ int i; ExcelWSheet = ExcelWBook.getSheet(SheetName); int rowCount = ExcelUtils.getRowCount(SheetName); for (i=0 ; i<rowCount; i++){ if (ExcelUtils.getCellData(i,colNum,SheetName).equalsIgnoreCase(sTestCaseName)){ break; } } return i; } // this is the method that I have trouble understanding, and its purpose public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception{ for(int i=iTestCaseStart;i<=ExcelUtils.getRowCount(SheetName);i++){ if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){ int number = i; return number; } } ExcelWSheet = ExcelWBook.getSheet(SheetName); int number=ExcelWSheet.getLastRowNum()+1; return number; } }
我理解代码中的所有方法(除了一个),例如:getCellData()返回特定行和列中的数据。 getRowCount()返回表单中的行数。 getRowContains()返回特定数据的行。
我不承认的方法是“getTestStepsCount”的最后一个方法。 我试过一遍又一遍地阅读,但我不明白。 这看起来和getRowCount方法非常相似,但同时不是。
有人可以在代码阅读方面更有经验,请帮助我理解它吗?
您的方法只是检查指定表格的指定列中的每一行。
一步步:
方法参数:
- stringSheetName =>validation发生的工作表的名称
- String sTestCaseID =>你正在寻找的string
- int iTestCaseStart =>用作search的偏移量的行数
方法的步骤:
- 要循环执行search,请返回结果(如果find)
- 如果循环中没有结果,则返回另一个值
步骤1的细节:
for(int i=iTestCaseStart;i<=ExcelUtils.getRowCount(SheetName);i++){ if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){ int number = i; return number; } }
这里的for
循环在你的偏移量iTestCaseStart
和你的工作表的行数之间迭代。
对于此循环中的每一行,它将检查行i
和列Constants.Col_TestCaseID
单元格内的数据是否与sTestCaseID
的值不同。
- 如果它们不同,则该方法将得到该行的索引(这是
i
的值)。 - 如果不是,继续迭代
如果在最后一行之前没有find不同的值,则循环结束,然后执行第2步。
步骤2的细节:
ExcelWSheet = ExcelWBook.getSheet(SheetName); int number=ExcelWSheet.getLastRowNum()+1; return number;
如果进入第二步,方法将返回最后一行+1的索引
补充评论
在这个代码中可以做许多改进…