如何使用java从一个工作簿中继续阅读多个工作表?

时间:2016-07-30 11:18:09

标签: java excel jexcelapi

我在一个工作簿中有多个工作表,它也会读取每一行。

但是每次它开始从第一行开始读取工作表,并且因此当它开始设置/写入案例的状态时,它再次开始设置第一行的结果意味着它与每个案例的结果重叠行。

另外,对于读写表格,我使用的是Jxl api。

以下是逻辑代码,任何人都可以告诉我如何解决它:

    private void handleScenario(String scenarioName) throws Exception {
        ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH);
        testScenarios.setSheetName(scenarioName);
        testScenarios.columnData();    

        int rowWorkBook1 = testScenarios.rowCount();
        System.out.println("Total Rows1: "+ rowWorkBook1);

        for (int j = 1; j < rowWorkBook1; j++) {
            String sno = testScenarios.readCell(testScenarios.getCell("Sno"), j); // SendKey
            String testCaseDescription = testScenarios.readCell(testScenarios.getCell("TestCaseDescription"), j);
            String framWork = testScenarios.readCell(testScenarios.getCell("FrameworkName"), j);
            String operation = testScenarios.readCell(testScenarios.getCell("Operation"), j); // SendKey
            String value = testScenarios.readCell(testScenarios.getCell("Value"), j);

            handleObjects(operation, value, framWork);
            boolean bTestCaseStepStatus = handleObjects(operation,value, framWork);

            generateReport(bTestCaseStepStatus, scenarioName, sno,testCaseDescription, j);
        }
    }

private boolean generateReport(boolean bTestCaseStepStatus,
            String testSuiteName, String SNO, String testCaseDescription, int j)
            throws RowsExceededException, WriteException, IOException {

        WritableData writableData = new WritableData(TEST_RESULT, testSuiteName, j, 2);

        // If sheet name is already exist then .... write data at row = row +1
        // else write directly .. means row = 1

        if (bTestCaseStepStatus) {
            //System.out.println("--------------"+bTestCaseStepStatus+"------------");
            System.out.println("SNo=" +SNO+ ",Test description=" + testCaseDescription + "Row ID: " + j);

            writableData.shSheet("Result", 2, j, "Pass");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        } else {
        //  System.out.println("--------------"+bTestCaseStepStatus+"------------");    
            System.out.println("SNo=" +SNO+ ",Test description=" + testCaseDescription + "Row ID: "+ j);            

            writableData.shSheet("Result", 2, j, "Fail");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        }

        return bTestCaseStepStatus;

        // Finally .... close Report file ... 
    }

    private boolean handleObjects(String operation, String value,
            String framWork) throws Exception {
        // Return true / False
        ExcelHandler objectRepository = new ExcelHandler(OBJECT_REPOSITORY_PATH, "OR");
        objectRepository.columnData();
        int rowCount = objectRepository.rowCount();
        //System.out.println("Total Rows in hadleObject=" + rowCount);
        boolean testStepStatus = false;

        for (int k = 1; k < rowCount; k++) {
            String frameWorkName = objectRepository.readCell(
                    objectRepository.getCell("FrameworkName"), k);
            String ObjectName = objectRepository.readCell(
                    objectRepository.getCell("ObjectName"), k);
            String Locator = objectRepository.readCell(
                    objectRepository.getCell("Locator"), k); // SendKey

            //System.out.println("FrameWorkNameV=" + frameWorkName
            //      + ",ObjectName=" + ObjectName + ",Locator=" + Locator);

            if (framWork.equalsIgnoreCase(frameWorkName)) {
                testStepStatus = operateWebDriver(operation, Locator, value,
                        ObjectName);

            }
        }

        return testStepStatus;
    }

2 个答案:

答案 0 :(得分:0)

您尚未在generateReport()中引用您的评论:

// If sheet name is already exist then .... write data at row = row +1
// else write directly .. means row = 1

请将其更改为:

ExcelHandler countRowsHandler = new ExcelHandler(TEST_RESULT);
countRowsHandler.setSheetName(testSuiteName);
countRowsHandler.columnData();
int existingRowCount= countRowsHandler.rowCount();
j = j + existingRowCount

<强>解释

我从您在此提供的文档中推断出这一点:

How to check in workbook sheet exist or not using JXL in selenium webdriver?

WritableData.shSheet(String strSheetName, int iColumnNumber, int iRowNumber, String strData)

接受行号,作为第三个参数。在

writableData.shSheet("Result", 2, j, "Pass");
writableData.shSheet("Result", 2, j, "Fail"); 

你提供j,这是一个“行ID”。这个j来自

for (int j = 1; j < rowWorkBook1; j++) {

所以对于handleScenario()的每次新调用,它总是从1开始,覆盖以前的运行。您需要找出已有的行数。从您的代码判断(没有引用Internet上记录的此方法),您可以使用ExcelHandler和.rowCount()

执行此操作

请考虑重构和清理整个代码库,这真是一团糟。使用过时的库(jxl),奇怪的变量命名和注释将使任何局外人都能理解这里发生的事情。

答案 1 :(得分:-1)

我通过编写3行以下来获得解决方案:

  1. 声明public int rowNumber = 0;
  2. 请参阅下面的handleScenario()方法:

    private void handleScenario(String scenarioName)抛出异常{

        ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH);
        testScenarios.setSheetName(scenarioName);
        testScenarios.columnData();
        int rowWorkBook1 = testScenarios.rowCount();
        System.out.println("Total Rows1: "+ rowWorkBook1);
    
        for (int j = 1; j < rowWorkBook1; j++) {
            String sno = testScenarios
                    .readCell(testScenarios.getCell("Sno"), j); // SendKey
            String testCaseDescription = testScenarios.readCell(
                    testScenarios.getCell("TestCaseDescription"), j);
    
            String framWork = testScenarios.readCell(
                    testScenarios.getCell("FrameworkName"), j);
            String operation = testScenarios.readCell(
                    testScenarios.getCell("Operation"), j); // SendKey
            String value = testScenarios.readCell(
                    testScenarios.getCell("Value"), j);
                        boolean bTestCaseStepStatus = handleObjects(operation, value,
                    framWork);
            generateReport(bTestCaseStepStatus, scenarioName, sno,
                    testCaseDescription, j+rowNumber);
    
            // Report
        }
         rowNumber = rowWorkBook1;
    }