Friday 22 May 2015

Apache POI:: Using the excel sheet for storing the values and using POI accessing the excel sheet and fetching the values.

package TestNG;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
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.Test;

public class AcceptInputFromPoiTest {
  @Test
  public void startExec() throws EncryptedDocumentException, InvalidFormatException, IOException {
 //Step 1: Giving the path to the workbook
 FileInputStream fis = new FileInputStream("C:\\Users\\NHCE\\Desktop\\MyInput.xlsx");

 //Step 2: Opening the workbook
 Workbook wb = WorkbookFactory.create(fis);

 //Step 3: Opening the respective sheet
 Sheet sh = wb.getSheet("Sheet1");

 //Step 4 : Finding the row
 Row rw = sh.getRow(1);

 //Step 5 : Finding the respective cell

 String userName = rw.getCell(2).getStringCellValue();
 String passWord = rw.getCell(3).getStringCellValue();
 String Cust = rw.getCell(4).getStringCellValue();

 System.out.println(userName+" "+passWord+" "+Cust);

  }
}

No comments:

Post a Comment