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
-
Read XL with HSSF
-
Read XL with XSSF
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.xlsfiles (Excel 97–2003 format)XSSFWorkbook→ for.xlsxfiles (Excel 2007 and later)
🔹 Step-by-Step Explanation
- Import Required Classes
The program imports classes from theorg.apache.poipackage such as:Workbook,Sheet,Row, andCellfor working with Excel structure.HSSFWorkbook(orXSSFWorkbook) to handle specific Excel file formats.FileInputStreamfor reading the file from disk.
- Specify the Excel File Path
The file to be read is passed to aFileInputStream:FileInputStream file = new FileInputStream("TestData.xls");This opens the Excel file so it can be processed by the program. - Create a Workbook Instance
The workbook represents the entire Excel file in memory.- For
.xlsfiles:Workbook workbook = new HSSFWorkbook(file); - For
.xlsxfiles:Workbook workbook = new XSSFWorkbook(file);
- For
- Access a Specific Sheet
Excel workbooks can contain multiple sheets.
The first sheet is usually accessed using:Sheet sheet = workbook.getSheetAt(0); - 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 usingcell.toString().
- Close the Workbook and File
To free up system resources, both the workbook and file stream are closed:workbook.close(); file.close(); - Handle Exceptions
The code is wrapped in atry-catchblock to catch and print any errors (e.g., file not found, I/O errors).
✅ Key Points
| Aspect | Description |
|---|---|
| Workbook | Represents the Excel file |
| Sheet | Represents a tab (worksheet) in the Excel file |
| Row | Represents a single row in the sheet |
| Cell | Represents an individual cell containing data |
| HSSFWorkbook | Used for .xls files |
| XSSFWorkbook | Used for .xlsx files |
| Loop Structure | Reads 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.