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:
wb = WorkbookFactory.create(fis);

Watch the demo here

Hope this helps!

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our, Thanks!

3 Responses

  1. Siva says:


    I want code for read & write excel data.

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

    Please provide the code.

  2. sunilpatro1985 says:

    Hi Siva,
    You can refer
    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 *