How to Write Excel Files Using Apache POI In Selenium WebDriver


Let’s see how to Read excel files using Apache POI in Selenium WebDriver:
Step 1– Download Apache POI jar file
Step 2– Add download jar files to Project build path(Follow the below navigation).
Select Project and Right click on the Project – Go to ‘Build path’ – Go to ‘Configure build path’ – Click on ‘lib’ section – Add external jar
Below mentioned code shows how to write data into excel file in selenium sing Apache POI.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WritingDataintoExcel {

public static void main(String[] args) throws IOException {

FileOutputStream file = new FileOutputStream("C://SeleniumPractice//testing.xlsx");

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("data");

for (int i = 0; i <= 5; i++) {
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < 3; j++) {
row.createCell(j).setCellValue("abcdef");
}
}

workbook.write(file);
System.out.println("writing excel is completed");

}

}

Followers