Automate Excel File(Read & Write) Functionality for Selenium Java with Limitations

Deepak Jha
3 min readSep 21, 2023

--

To automate Excel file read and write functionality for Selenium Java, you can use the Apache POI library.

POI is a free and open-source library that provides a Java API for reading and writing various types of Microsoft Office files, including Excel.

To use POI in your Selenium Java project, you will need to add the following JAR files to your classpath:

  • poi-ooxml-4.1.2.jar
  • poi-4.1.2.jar
  • poi-excelant-4.1.2.jar
  • poi-scratchpad-4.1.2.jar
  • xmlbeans-2.6.0.jar

Once you have added the POI JAR files to your classpath, you can start reading and writing Excel files using the following steps:

  1. Create a Workbook object using the WorkbookFactory class.
  2. Get the Sheet object for the sheet that you want to read or write to.
  3. Use the Row and Cell classes to read and write data from the sheet.
  4. Close the Workbook object when you are finished.

Here is an example of how to read data from an Excel file using POI and Selenium Java:

Java

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ReadExcelFile {    public static void main(String[] args) throws Exception {        // Create a WebDriver object
WebDriver driver = new ChromeDriver();
// Create a Workbook object
Workbook workbook = WorkbookFactory.create(new FileInputStream("C:\\path\\to\\excel\\file.xlsx"));
// Get the Sheet object for the sheet that you want to read from
Sheet sheet = workbook.getSheetAt(0);
// Iterate over the rows and cells in the sheet
for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum() + 1; j++) {
Cell cell = row.getCell(j);
// Get the value of the cell
String value = cell.getStringCellValue();
// Do something with the value of the cell
System.out.println(value);
}
}
// Close the Workbook object
workbook.close();
// Close the WebDriver object
driver.quit();
}
}

Use code with caution.

Here is an example of how to write data to an Excel file using POI and Selenium Java:

Java

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WriteExcelFile {    public static void main(String[] args) throws Exception {        // Create a WebDriver object
WebDriver driver = new ChromeDriver();
// Create a Workbook object
Workbook workbook = WorkbookFactory.create(new FileInputStream("C:\\path\\to\\excel\\file.xlsx"));
// Get the Sheet object for the sheet that you want to write to
Sheet sheet = workbook.getSheetAt(0);
// Create a new row
Row row = sheet.createRow(0);
// Create a new cell in the row
Cell cell = row.createCell(0);
// Set the value of the cell
cell.setCellValue("This is a new value");
// Write the changes to the Workbook object
workbook.write(new FileOutputStream("C:\\path\\to\\excel\\file.xlsx"));
// Close the Workbook object
workbook.close();
// Close the WebDriver object
driver.quit();
}
}

Use code with caution.

Limitations of automating Excel file read and write functionality for Selenium Java with Apache POI:

  • POI does not support all Excel features, such as macros and charts.
  • POI can be slow to read and write large Excel files.
  • POI can be difficult to use, especially for complex Excel files.

If you need to automate the reading and writing of complex Excel files, you may want to consider using a commercial Excel automation library.

--

--

Deepak Jha
Deepak Jha

Written by Deepak Jha

Author in progress, News Junkie, Technophile-Neophile, Compulsive Overthinker, Full Stack QA

No responses yet