steps:
1.create maven project
2.attach selenium and chrome drivers
3.set dependency in pom file for Apache POI
4.attach one excel file in the project
METHOD 1:
file 1: SunWebLink.java
package excelExportAndFileIO;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SunWebLink {
public static void main(String...strings) throws IOException{
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get("http://vishnuyardinichari.blogspot.in/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");
WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile();
System.out.println("calling write program");
/*
ArrayList<String> data=new ArrayList<String>();
data.add("links");
*/
System.out.println("writting links");
for(WebElement link:links){
// System.out.println(link.getAttribute("href"));
objExcelFile.writeExcel(System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx","sheet1",link.getAttribute("href"));
}
//System.out.println("complete writing links");
System.out.println("URL-----"+links.size());
}
}
1.create maven project
2.attach selenium and chrome drivers
3.set dependency in pom file for Apache POI
4.attach one excel file in the project
METHOD 1:
file 1: SunWebLink.java
package excelExportAndFileIO;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SunWebLink {
public static void main(String...strings) throws IOException{
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get("http://vishnuyardinichari.blogspot.in/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");
WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile();
System.out.println("calling write program");
/*
ArrayList<String> data=new ArrayList<String>();
data.add("links");
*/
System.out.println("writting links");
for(WebElement link:links){
// System.out.println(link.getAttribute("href"));
objExcelFile.writeExcel(System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx","sheet1",link.getAttribute("href"));
}
//System.out.println("complete writing links");
System.out.println("URL-----"+links.size());
}
}
file 2:WriteGuru99ExcelFile.java
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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;
public class WriteGuru99ExcelFile {
public void writeExcel(String filePath,String sheetName,String string) throws IOException{
//Create a object of File class to open xlsx file
File file = new File(filePath);
//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 substing and getting only extension name
guru99Workbook = new XSSFWorkbook(inputStream);
//Read excel sheet by sheet name
Sheet sheet = guru99Workbook.getSheet(sheetName);
//Get the current count of rows in excel file
int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
//Get the first row from the sheet
//Row row = sheet.getRow(0);
//Create a new row and append it at last of sheet
Row newRow = sheet.createRow(rowCount+1);
//Create a loop over the cell of newly created Row
for(int j = 0;j<2300; j++){
//Fill data in row
Cell cell = newRow.createCell(j);
//System.out.println("Data to write-"+string);
cell.setCellValue(string);
}
//Close input stream
//Create an object of FileOutputStream class to create write data in excel file
FileOutputStream outputStream = new FileOutputStream(file);
//write data in the excel file
guru99Workbook.write(outputStream);
//close output stream
outputStream.close();
inputStream.close();
}
public static void main(String...strings) throws IOException{
//Create an array with the data in the same order in which you expect to be filled in excel file
String[] valueToWrite = {"Mr. E","Noida"};
//Create an object of current class
WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile();
//Write the file using file name , sheet name and the data to be filled
// objExcelFile.writeExcel(System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx","sheet1",valueToWrite);
}
}
RUN:
run the application at java application
No comments:
Post a Comment