How to read excel file in selenium java

We can read Excel files in Selenium with Java by using external libraries such as JXL and Apache POI. The JXL library supports only .xls files, while Apache POI can handle both .xls and .xlsx formats. In Apache POI, we use the HSSF classes to work with .xls files and the XSSF classes to work with .xlsx files.

In the following examples, I will demonstrate how to read both .xls and .xlsx files using the Apache POI library.

Read Excel

import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

public class ReadXlsFile {
public static void main(String[] args) {
try {
// Specify the Excel file path
FileInputStream file = new FileInputStream("TestData.xls");

// Create Workbook instance for .xls file
Workbook workbook = new HSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);

// Loop through all rows and cells
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell.toString() + "\t");
}
System.out.println();
}

workbook.close();
file.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

public class ReadXlsxFile {
public static void main(String[] args) {
try {
// Specify the Excel file path
FileInputStream file = new FileInputStream("TestData.xlsx");

// Create Workbook instance for .xlsx file
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);

// Loop through all rows and cells
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell.toString() + "\t");
}
System.out.println();
}

workbook.close();
file.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

📘 Common Explanation for Reading Excel Files Using Apache POI (HSSF & XSSF)

This program demonstrates how to read data from an Excel file in Java using the Apache POI library.
The logic remains the same for both file formats — the only difference is the workbook class used:

  • HSSFWorkbook → for .xls files (Excel 97–2003 format)
  • XSSFWorkbook → for .xlsx files (Excel 2007 and later)

🔹 Step-by-Step Explanation

  1. Import Required Classes
    The program imports classes from the org.apache.poi package such as:
    • Workbook, Sheet, Row, and Cell for working with Excel structure.
    • HSSFWorkbook (or XSSFWorkbook) to handle specific Excel file formats.
    • FileInputStream for reading the file from disk.
  2. Specify the Excel File Path
    The file to be read is passed to a FileInputStream: FileInputStream file = new FileInputStream("TestData.xls"); This opens the Excel file so it can be processed by the program.
  3. Create a Workbook Instance
    The workbook represents the entire Excel file in memory.
    • For .xls files: Workbook workbook = new HSSFWorkbook(file);
    • For .xlsx files: Workbook workbook = new XSSFWorkbook(file);
  4. Access a Specific Sheet
    Excel workbooks can contain multiple sheets.
    The first sheet is usually accessed using: Sheet sheet = workbook.getSheetAt(0);
  5. Iterate Through Rows and Cells
    A nested loop is used to read all rows and cells in the sheet: for (Row row : sheet) { for (Cell cell : row) { System.out.print(cell.toString() + "\t"); } System.out.println(); }
    • The outer loop iterates through each row.
    • The inner loop iterates through all the cells in that row.
      Each cell’s value is printed using cell.toString().
  6. Close the Workbook and File
    To free up system resources, both the workbook and file stream are closed: workbook.close(); file.close();
  7. Handle Exceptions
    The code is wrapped in a try-catch block to catch and print any errors (e.g., file not found, I/O errors).

✅ Key Points

AspectDescription
WorkbookRepresents the Excel file
SheetRepresents a tab (worksheet) in the Excel file
RowRepresents a single row in the sheet
CellRepresents an individual cell containing data
HSSFWorkbookUsed for .xls files
XSSFWorkbookUsed for .xlsx files
Loop StructureReads all rows and columns dynamically

💡 Tip for Automation (Selenium)

In Selenium automation frameworks, this approach is often used to:

  • Read test data from Excel for data-driven testing.
  • Retrieve usernames, passwords, URLs, or test input data.
  • Validate test output against expected data stored in Excel.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x