0% found this document useful (0 votes)
6 views

Excel Reader Selenium

The provided Java method retrieves data from a specified Excel sheet based on given row and column names. It opens the file, locates the specified row and column, and returns the corresponding cell's data. If the row or column is not found, or if an error occurs, it returns appropriate error messages.

Uploaded by

myanime1994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Excel Reader Selenium

The provided Java method retrieves data from a specified Excel sheet based on given row and column names. It opens the file, locates the specified row and column, and returns the corresponding cell's data. If the row or column is not found, or if an error occurs, it returns appropriate error messages.

Uploaded by

myanime1994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

public static String getDataFromSheet(String filePath, String sheetName, String rowName, String

columnName) {

try {

FileInputStream file = new FileInputStream(new File(filePath));

Workbook workbook = WorkbookFactory.create(file);

Sheet sheet = workbook.getSheet(sheetName);

int rowIndex = -1;

int columnIndex = -1;

// Finding the row index based on the row name

for (Row row : sheet) {

Cell cell = row.getCell(0); // Assuming the row name is in the first column (column 0)

if (cell.getStringCellValue().equals(rowName)) {

rowIndex = row.getRowNum();

break;

// Finding the column index based on the column name in the first row

Row headerRow = sheet.getRow(0); // Assuming the column names are in the first row

for (Cell cell : headerRow) {

if (cell.getStringCellValue().equals(columnName)) {

columnIndex = cell.getColumnIndex();

break;

}
}

if (rowIndex != -1 && columnIndex != -1) {

Row row = sheet.getRow(rowIndex);

Cell cell = row.getCell(columnIndex);

String data = cell.getStringCellValue();

workbook.close();

file.close();

return data;

} else {

workbook.close();

file.close();

return "Row or Column not found.";

} catch (IOException e) {

e.printStackTrace();

return "An error occurred.";

You might also like