如何在apache poi

时间:2015-06-08 23:11:55

标签: java excel apache-poi

请帮忙!作为Selenium框架的新手,我有一个方法可以接受5个参数来预订派对;它使用dataprovider从excel文件中读取。问题是(如下所示)它使用仅支持XLS文件(excel 2003或更早版本)的JXL导入。我需要帮助使用类似的代码来使用Apache Poi,以便它支持XLSX和新版本的excel(2007+)。有人能帮帮我吗?

package com.suite1;
import util.TestUtil;
import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;



public class CreatePartyTest extends TestBase1 {
    Workbook wb;     
    Sheet sh1;
    int numrow;

    @BeforeTest
    public void beforeTest() throws IOException
    {   
        initialize();
        if (TestUtil.isSkip("CreatePartyTest"))
        {
            throw new SkipException("Skipping test, check run mode");       
        }
        dr.get(CONFIG.getProperty("testSiteName"));
        getobject("signin_link").click();
        getobject("username_Signin_input").sendKeys("alexy.dsouza");
        getobject("password_input").sendKeys("testing123");
        getobject("submit_button").click();

    }
    @Test(dataProvider="Partydata")
    public void createParty (String Partyname, String Date, String Firstname, String Lastname, String email, String mobile) throws InterruptedException
    {
        getobject("party_link").click();
        getobject("start_party_link").click();
        getobject("partyname_input").sendKeys(Partyname);
        getobject("partydate_input").sendKeys(Date);
        getobject("hostfirstname_input").sendKeys(Firstname);
        getobject("hostlastname_input").sendKeys(Lastname);
        getobject("hostemail_input").sendKeys(email);
        getobject("hostmobile_input").sendKeys(mobile);
        getobject("make_reservation").click();
    }
    //source 
        @DataProvider(name="Partydata")
        public Object[][] TestDataFeed(){
        try {
        // load workbook: this is where i store my excel
        wb=Workbook.getWorkbook(new File("C://Workspace//Max//excelfiles//Partydata.xls"));
        // load sheet in my case I am referring to first sheet only
        sh1= wb.getSheet(0);
        // get number of rows so that we can run loop based on this
        numrow=  sh1.getRows();
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        // Create 2 D array and pass row and columns
        Object [][] Accountdata=new Object[numrow-1][sh1.getColumns()];
        // This will run a loop and each iteration it will fetch new row
        for(int i=0,j=1;i<numrow-1;i++){
            // Fetch first row Accountname
                Accountdata[i][0]=sh1.getCell(0,j).getContents();
            // Fetch first row BankName
                Accountdata[i][1]=sh1.getCell(1,j).getContents();
            // Fetch everything else before an empty column
                Accountdata[i][2]=sh1.getCell(2,j).getContents();
                Accountdata[i][3]=sh1.getCell(3,j).getContents();
                Accountdata[i][4]=sh1.getCell(4,j++).getContents();

        }// Return 2d array object so that test script can use the same
        return Accountdata;
        }


}

1 个答案:

答案 0 :(得分:0)

我无法解决您的确切查询,但是您可以从我仅使用.xlsx工作簿的代码中获取引用,并且对我来说工作正常。 我可以从excel sheet(.xlsx)读取数据。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProvidersConcept {


    @DataProvider(name="Excelsheet")
    public Object[][] readData() throws Exception
    {
        File f = new File("C:/Users/Vikrant/Documents/MavenTesting.xlsx");
        FileInputStream fis = new FileInputStream(f);
        XSSFWorkbook workBook = (XSSFWorkbook) WorkbookFactory.create(fis);
        XSSFSheet sheet=workBook.getSheet("Sheet1");
        Object array[][]=new Object[2][2];
        for(int i =0;i<2;i++)
        {
            for( int j=0;j<2;j++)
            {
                array[i][j]=sheet.getRow(i).getCell(j).toString();

            }
        }
        return array;



    }

    @Test(dataProvider="Excelsheet")
    public void testData(String Username , String password)
    {
        System.out.println(Username);
        System.out.println("Username tested successfully");
        System.out.println(password);
        System.out.println("password tested successfully");
    }
}

enter code here