Read/Write .xls and .xlsx with one set of code using POI – WorkbookFactory

As we discussed in earlier post: Read/Write excel using Apache POI,
we have to use:
For handling .xls fileswe use HSSF (Horrible SpreadSheet Format) related POI classes.

For handling .xlsx files, we use XSSF (XML SpreadSheet Format) related POI classes.

On this post, how we can perform excel operations with one set code for both excel formats .xls and .xlsx.
For this we have a class provided from Apache POI WorkbookFactory, which auto detects appropriate kind of Workbook (HSSFWorkbook or XSSFWorkbook) depending on the excel formats .xls or .xlsx.
Let’s jump to the code implementation

As you can see, only line changed from earlier post, code changed for line:

Hope this helps!

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

3 Responses

  1. Siva says:

    Hi,

    I want code for read & write excel data.

    But in that i am using some usernames & passwords , write the status also for
    webapplication.

    Please provide the code.

  2. sunilpatro1985 says:

    Hi Siva,
    You can refer http://qavalidation.com/2015/03/selenium-excel-read-and-write-apachepoi.html/
    for read/write and highlight the cell…

  3. Philips says:

    Thank you for taking the time to provide us with your valuable information.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: