file name:ReadGuru99ExcelFile.java
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ReadGuru99ExcelFile {
// public String urlLink;
public void readExcel(String sheetName) throws IOException{
//Create a object of File class to open xlsx file
String filePath = System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
// File file = new File(filePath+"\\"+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(filePath);
Workbook guru99Workbook = null;
//Find the file extension by spliting file name in substring and getting only extension name
guru99Workbook = new XSSFWorkbook(inputStream);
//Read sheet inside the workbook by its name
Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);
//Find number of rows in excel file
int rowCount = guru99Sheet.getLastRowNum();
//Create a loop over all the rows of excel file to read it
System.out.println("Row Count"+rowCount);
for (int i = 0; i < rowCount; i++) {
Row row = guru99Sheet.getRow(i);
getCellData(row);}
}
public static String getCellData(Row row){
String value="";
try{
if(row.getCell(0).getCellType()!=Cell.CELL_TYPE_BLANK){
value= row.getCell(0).getStringCellValue();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get(value);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
}
}catch(NullPointerException e){
//return "";
}
return value;
}
// System.out.println("Current row--"+i);
/* String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
String baseUrl = getCellData(row);
driver.get(baseUrl);*/
// driver.get(getCellData(google.com));
// String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
// System.out.println( getCellData(row));
//urlLink= getCellData(row);
// driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
//rowCount++;
// System.out.println(rowCount);
//Main function is calling readExcel function to read data from excel file
public static void main(String...strings) throws IOException{
//Create a object of ReadGuru99ExcelFile class
ReadGuru99ExcelFile objExcelFile = new ReadGuru99ExcelFile();
//Prepare the path of excel file
//Call read file method of the class to read data
objExcelFile.readExcel("Sheet1");
}
}
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ReadGuru99ExcelFile {
// public String urlLink;
public void readExcel(String sheetName) throws IOException{
//Create a object of File class to open xlsx file
String filePath = System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
// File file = new File(filePath+"\\"+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(filePath);
Workbook guru99Workbook = null;
//Find the file extension by spliting file name in substring and getting only extension name
guru99Workbook = new XSSFWorkbook(inputStream);
//Read sheet inside the workbook by its name
Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);
//Find number of rows in excel file
int rowCount = guru99Sheet.getLastRowNum();
//Create a loop over all the rows of excel file to read it
System.out.println("Row Count"+rowCount);
for (int i = 0; i < rowCount; i++) {
Row row = guru99Sheet.getRow(i);
getCellData(row);}
}
public static String getCellData(Row row){
String value="";
try{
if(row.getCell(0).getCellType()!=Cell.CELL_TYPE_BLANK){
value= row.getCell(0).getStringCellValue();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get(value);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
}
}catch(NullPointerException e){
//return "";
}
return value;
}
// System.out.println("Current row--"+i);
/* String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
String baseUrl = getCellData(row);
driver.get(baseUrl);*/
// driver.get(getCellData(google.com));
// String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
// System.out.println( getCellData(row));
//urlLink= getCellData(row);
// driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
//rowCount++;
// System.out.println(rowCount);
//Main function is calling readExcel function to read data from excel file
public static void main(String...strings) throws IOException{
//Create a object of ReadGuru99ExcelFile class
ReadGuru99ExcelFile objExcelFile = new ReadGuru99ExcelFile();
//Prepare the path of excel file
//Call read file method of the class to read data
objExcelFile.readExcel("Sheet1");
}
}
No comments:
Post a Comment