Monday, April 15, 2019

Apache POI Workbook Factory in Java

 package test;  
 import java.io.FileInputStream;  
 import org.apache.poi.ss.usermodel.Sheet;  
 import org.apache.poi.ss.usermodel.Workbook;  
 import org.apache.poi.ss.usermodel.WorkbookFactory;  
 import org.testng.annotations.DataProvider;  
 import org.testng.annotations.Test;  
 public class DataExcel {  
      private static String[][] users;  
      public static String[][] getData() throws Exception {  
           Workbook workbook = WorkbookFactory.create(new FileInputStream("C:\\Users\\Mitturaj.h\\Desktop\\data.xlsx"));  
           Sheet sheet = workbook.getSheet("Sheet1");  
           int noOfRows = sheet.getLastRowNum() + 1;  
           System.out.println(noOfRows);  
           users = new String[noOfRows][2];  
           for (int i = 0; i < noOfRows; i++) {  
                for (int j = 0; j < 2; j++) {  
                     String user = sheet.getRow(i).getCell(j).getStringCellValue();  
                     users[i][j] = user;  
                }  
           }  
           return users;  
      }  
      @DataProvider(name = "users")  
      public static Object[][] getUsers() throws Exception {  
           return getData();  
      }  
      @Test(dataProvider = "users")  
      public void login(String username, String pass) {  
           System.out.println(username + " " + pass);  
      }  
 }  
           <dependency>  
                <groupId>org.apache.poi</groupId>  
                <artifactId>poi-ooxml</artifactId>  
                <version>3.17</version>  
           </dependency>  
           <dependency>  
                <groupId>org.apache.poi</groupId>  
                <artifactId>poi</artifactId>  
                <version>3.17</version>  
           </dependency>  
           <dependency>  
                <groupId>org.apache.poi</groupId>  
                <artifactId>ooxml-schemas</artifactId>  
                <version>1.1</version>  
           </dependency>  
           <dependency>  
                <groupId>org.testng</groupId>  
                <artifactId>testng</artifactId>  
                <version>6.8</version>  
                <scope>test</scope>  
           </dependency>  

No comments:

Post a Comment