Thursday, July 18, 2019

Read Excel FIle using Java Code in Selenium Automation

Below code is compile and Executed and it is working fine using below JAR files.



which are download from https://poi.apache.org/download.html
Note: Above mentioned specific version of file might not available in future.  So, NO WORRIES you can download the latest POI ZIP file using https://poi.apache.org/download.html

/**
 * @author Mano
 *
 */
package javaselenium;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile {

    public void readExcelFile(String filePath,String fileName,String sheetName) throws IOException{

    //Create an object of File class to open xlsx file
    File file =    new File(filePath+"\\"+fileName);

    //Create an object of FileInputStream class to read excel file
    FileInputStream inputStream = new FileInputStream(file);
    Workbook sampleWorkbook = null;

    //Find the file extension by splitting file name in substring  and getting only extension name
    String fileExtensionName = fileName.substring(fileName.indexOf("."));

    //Check condition if the file is xlsx file
    if(fileExtensionName.equals(".xlsx")){
    //If it is xlsx file then create object of XSSFWorkbook class
    sampleWorkbook = new XSSFWorkbook(inputStream);
    }

    //Check condition if the file is xls file
    else if(fileExtensionName.equals(".xls")){
        //If it is xls file then create object of HSSFWorkbook class
        sampleWorkbook = new HSSFWorkbook(inputStream);
    }

    //Read sheet inside the workbook by its name
    Sheet sampleSheet = sampleWorkbook.getSheet(sheetName);

    //Find number of rows in excel file
    int rowCount = sampleSheet.getLastRowNum()-sampleSheet.getFirstRowNum();

    //Create a loop over all the rows of excel file to read it
    for (int i = 0; i < rowCount+1; i++) {
        Row row = sampleSheet.getRow(i);

        //Create a loop to print cell values in a row
        for (int j = 0; j < row.getLastCellNum(); j++) {
        String cellData="";
       
            //Print Excel data in console
        cellData = (String)row.getCell(j).toString();
            System.out.print(cellData+" || ");
        }

        System.out.println();
    } 

    }  

    //Main function is calling readExcel function to read data from excel file
    public static void main(String[] args) throws IOException{

    //Create an object of ReadExcelFile class
    ReadExcelFile objExcelFile = new ReadExcelFile();

    //Prepare the path of excel file
    String filePath = "E:\\Mano\\Java Selenium\\";

    //Call read file method of the class to read data
    objExcelFile.readExcelFile(filePath,"SampleExcel.xlsx","SampleSheet");

    }

}

No comments:

Post a Comment

Popular Posts