Posts

Showing posts with the label Apache POI

Generalized Apache POI script for reading and writing to .xlsx files

In this post, we will be looking to simplify the script mentioned in this post . Below script is generalized version so it can be used by other classes for reading and writing to .xlsx files. The code for reading and writing to .xlsx is made modular by putting them in different class and making read and write methods static. Any test that needs to read from excel file can just call the readFromXLSX() method and similarly any test that needs to write data to excel file, can just call the writeTestResultToXLSX() method and passing the appropriate parameters to it. Here's the class that contains the read and write methods: Below code makes use of Enumerations to restrict the value of test result to be either PASS or FAIL. This would make TestNG tests that will use these methods, not to write illegal values into the excel sheet.  import java.io.File ; import java.io.FileInputStream ; import java.io.FileNotFoundException ; import java.io.FileOutputStream ; import java....

Reading and writting data from .xlsx spreadsheet using Apache POI API

Image
Why to use Apache POI and not Jexcel API ? Below are the drawbacks of using Jexcel API for interacting with excel : Jexcel supports .xls format of MS excel, which is not the default option we get while saving excel sheets, nowadays. So most of the excel sheets are by default get saved in .xlsx format. Apache POI supports not only .xls but also .xlsx type of spreadsheets. We normally come across scenario such as - we want to read test data from the excel sheet and then write back to same excel sheet with some other related information. This can be achieved using Apache POI and not by Jexcel API as Jexcel does not support modifying a spreadsheet that is being used for reading. Jexcel API is no longer in development i.e. no future updates/enhancements are not possible but Apache POI is still in development and updates/enhancements are very much possible. So, in the long run, it'd be beneficial to use Apache POI in code rather than Jexcel API. Let's see a simple script t...