Menu Close
    Hire us Request quote Reading list Switch to dark mode

    Selenium Webdriver: Read and Write Data from Excel Sheet using Apache POI Libraries

    In the real word automation script, the test data shouldn’t be hardcoded. Some external resources (excel sheet, properties file, xml file, json file etc.) should be used to read or write data.

    In Selenium, Java provides some different classes or interfaces to perform file manipulation. Apache POI libraries are used to perform such operations. Some of the interfaces are given below to read or write data from the external resources:

    1. POIFS (Poor Obfuscation Implementation File System)
    2. HSSF(Horrible Spreadsheet Format)
    3. XSSF(XML Spreadsheet Format)
    4. HPSF(Horrible Property Set Format)
    5. HWPF(Horrible Word Processor Format)
    6. XWPF(XML Word Processor Format)
    7. HSLF(Horrible Slide Layout Format)
    8. HGDF(Horrible Diagram Format)
    9. HDBF(Horrible PuBlisher Format)

    To create or maintain Excel Workbooks , Apache POI provides ” Workbook” as super-interface of all classes. It belongs to org.apache.poi.ss.usermodel package. It uses WorkbookFactory class for creating the appropriate kind of Workbook (i.e. HSSFWorkbook or XSSFWorkbook). The two classes which implements “Workbook” interface are given below:

    1. HSSFWorkbook- This class methods are used to read/write data to Microsoft Excel file in .xls format.
    2. XSSFWorkbook- This class methods are used to read write data to Microsoft Excel and OpenOffice xml files in .xls or .xlsx format.

    Note: For Apache POI Libraries installation, please refer to the blog– https://webkul.com/blog/apache-poi-installation-to-project-library/

    Read Data From Excel Sheet:

    Below Code is written to read the data from the excel sheet:

    “Workbook” interface throws some exceptions which needs to be handled i.e. EncryptedDocumentException, InvalidFormatException, FileNotFoundException & IOException.

    Note: In Excel Sheet, there shouldn’t be any voided cell in-between the data fields, else the code will throw an error message at the time of execution & the data after the voided cell will be skipped.

    The above code can also be written as below for better understanding:

    Write Data to Excel Sheet:

    Below code is written to write the code to the excel sheet:

    In case you have any queries then feel free to ask in the comment section below.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close