CRUD -> Read excel {sheet, row, cell}

Read excel as FileInputStream

    @When("^I read the excel file \"(.*?)\"$")
    public void i_read_the_excel_file(String arg1) throws Throwable {
            workbook = new XSSFWorkbook(new FileInputStream(System.getProperty("user.dir")+"//"+"src//test//resources//"+arg1));
    }

Feature file defination

  Scenario: Read existing excel file print all its contents
    When I read the excel file "Sample.xlsx"
    Then I access a worksheet
    Then I access the rows and cells
    @Then("^I access a worksheet$")
    public void i_access_a_worksheet() throws Throwable {
        XSSFSheet sheet = workbook.getSheet("Sheet1");
    }

    @Then("^I access the rows and cells$")
    public void i_access_the_rows_and_cells() throws Throwable {
        XSSFSheet sheet = workbook.getSheet("Sheet1");
        for(int i=0;i<sheet.getPhysicalNumberOfRows();i++)
        {
            Row currentRow = sheet.getRow(i);
            for(int j=0;j<currentRow.getPhysicalNumberOfCells();j++)
            {
                Cell currentCell = currentRow.getCell(j);
                switch (currentCell.getCellType())
                {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(currentCell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(currentCell.getNumericCellValue() + "\t");
                        break;
                }
            }
            System.out.println("\n");

        }
    }

 

posted @ 2018-01-12 11:32  thisissweetcandy  阅读(43)  评论(0)    收藏  举报