Saturday, March 1, 2014

Reading and Writing Excel Sheet

Reading and Writing Excel Sheets:

While automating your tests using selenium, its quiet frequent to use excel sheet as Object Repository or test data sheet or may be a result sheet.
Here I am gonna tell you some ways to read / write excel sheet in Java for Selenium Web Driver.

There are various third party libs are available to handle excel operations, popular ones are POI and JXL.

Writing an Excel File Using JXL: 

Before you start using JXL, you need to download the latest version of jxl.jar and attach it to Java Build Path of your project.

ixl can be downloaded from http://www.quicklyjava.com/jexcel-jar-download/

This is sample code to write a 2 D Array in an excel sheet.


Few Points to remember while writing an excel sheet:

1. Always use sheet.addCell(string) method to write the contents in excel sheet.
2. Always remember to close the workbook after writing.
3. Always use Exception Handling and use these book.write(); book.close(); in Finally block so that even if there is any exception and code exception is broken then at least the previous data till exception is written in excel sheet otherwise there won't be any data in excel sheet.





Reading an Excel File Using JXL: 

Sample code to read an existing excel sheet:






No comments:

Post a Comment