How Selenium supports Writing of Excel File?
Writing of Excel file can be achieved with the help of API called JXL. Currently JXL only support xls type file.
Steps to download jxl2.6 jar file and configure in the Project
Step1: Go to the URL:-http://www.findjar.com/jar/net.sourceforge.jexcelapi/jars/jxl-2.6.jar.html and down load the jar file
Step 2: Extract the jar file at the location of download
Step3: On Eclipse>Go to Project and right Click on it >Go to build path>Select Configure build pat>Under Libraries ,click on Add External Jar to add the Jar file
Steps to Write data into the Excel File
Excel Sheet Output
Writing of Excel file can be achieved with the help of API called JXL. Currently JXL only support xls type file.
Steps to download jxl2.6 jar file and configure in the Project
Step1: Go to the URL:-http://www.findjar.com/jar/net.sourceforge.jexcelapi/jars/jxl-2.6.jar.html and down load the jar file
Step 2: Extract the jar file at the location of download
Step3: On Eclipse>Go to Project and right Click on it >Go to build path>Select Configure build pat>Under Libraries ,click on Add External Jar to add the Jar file
Steps to Write data into the Excel File
- Specify the Location of File where file is to be stored
- Create WorkBook
- Create Sheet
- Get Sheet
- Add Cell Data
- Write WorkBook
- Close WorkBook
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.asserts.*;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.*;
public class WriteExcel {
public static void main(String[] args) throws BiffException, IOException, WriteException {
File f = new File("D:/WriteData.xls");//Give a File Path
WritableWorkbook w = Workbook.createWorkbook(f);//Create WorkBook
w.createSheet("ashish", 0);//Create Sheet
WritableSheet s =w.getSheet(0);//Get Sheet
//Add Cell Data
Label data = new Label(0,0,"Test");
s.addCell(data);
//Write WorkBook
w.write();
//Close WorkBook
w.close();
}
}
Notes
1-Eclipse support one forward slash or two backward slash ,so we have used forward slash in order to create the file
2-createWorkbook method() creates a work book
3-WritableWorkbook class has a method createSheet() which is used to create a sheet within a workbook, this method has a parameter 1 as "Name of Sheet" which is ashish in this case and parameter 2 as index of sheet: Here index of first sheet is zero
4-Label is a class provided by JXL which creates a cell when added to a sheet.Its Parameters are
a) Parameter 1: Column number
b) Parameter 2: Row number
c) Parameter 3: Content to be added in cell
5-WritableSheet class has a method called addCell which is used to add cell data to the specified location mentioned in Label
2-createWorkbook method() creates a work book
3-WritableWorkbook class has a method createSheet() which is used to create a sheet within a workbook, this method has a parameter 1 as "Name of Sheet" which is ashish in this case and parameter 2 as index of sheet: Here index of first sheet is zero
4-Label is a class provided by JXL which creates a cell when added to a sheet.Its Parameters are
a) Parameter 1: Column number
b) Parameter 2: Row number
c) Parameter 3: Content to be added in cell
5-WritableSheet class has a method called addCell which is used to add cell data to the specified location mentioned in Label
Excel File Created Using JXL |