Read Excel through Java using XSSF and SAX (Apache POI)

read excel through java using xssf and saxRead Excel through Java using XSSF and SAX (Apache POI); ready to use Java library/classes for reading XLSX file into Java List Object or process row wise data conveniently.  You may think how this article differs from plethora of articles over internet.

Of-course it has uniqueness within:

  • Ready to use Java Classes for reading excel Workbook, as simple!
  • Advantages of XSSF Streaming API type (aka SAX); CPU & Memory efficiency for reading
  • Simply described usage & benefits via Test classes ExcelWorkSheetHandlerTest &  ExcelWorkSheetRowCallbackHandlerTest
  • Javadocs for ExcelReader, ExcelWorkSheetHandler<T> &  ExcelXSSFRowCallbackHandler
  • Excel Template Header Validation (Row Zero is header)
  • Skip No. of rows
  • Excel Callback handlers for in-depth processing/handling excel row values – ExcelSheetCallback and ExcelRowContentCallback

Obtaining ExcelReader Library & Source Code

For Maven based project can easily refer below pom definition:

Need an excelReader jar file instead, click here to download.

ExcelReader project scoure code as excelReader-master.zip, click here to download OR alternative you can clone it from GitHub-


How to Use ExcelReader

It is simple and easy to utilize and take advantage of theses classes.  Will describe usage in two steps:

Note: ExcelWorkSheetHandler implements the Interface XSSFSheetXMLHandler.SheetContentsHandler for processing the excel work sheet.

Example Code from ExcelWorkSheetHandlerTest.java


Example code from ExcelWorkSheetRowCallbackHandlerTest.java

  • http://mymtc.midlandstech.edu Jim

    Thanks for the sample code. It only took a tweak or two (change the map values and modify the PersonVO setters and getters)to be able to read my .xlsx file. Built using NetBeans 7.2. Needed two jar files not included with Tomcat and NetBeans dom4j-1.x.jar and xmlbeans-2.x.0.jar

    • http://www.myjeeva.com Jeevanandam Madanagopal

      You’re Welcome Jim, thanks for the details of built using NetBeans.

      Cheers,
      Jeeva

      • Christopher Daniel

        Hi Jeevan,
        Your example is really awesome. But the example ExcelRowCallbackDemo.java is giving only the first sheet result

        • http://myjeeva.com/ Jeevanandam M.

          Thanks. I just noticed that. Will add support for sheet wise and all sheets processing in day or two.

          Will be available in v1.3 :)

        • http://myjeeva.com/ Jeevanandam M.

          @disqus_EA7G6Oixi3:disqus – excelReader v1.2 version is released. I have published the library to Maven Central Repo too.

  • Ashwath

    Hi Jeeva,

    Could you please let me know, what are the library files I need to use in-order to use this code snippet.

    Thanks

    Ashwath

    • http://www.myjeeva.com Jeevanandam Madanagopal

      Hello Ashwath – Code files are in GitHub repo. Please refer pom.xml

      Cheers,

      Jeeva

  • Akshay

    Hi Jeeva,

    I need read all type of excel file and then convert into CSV. Which API should i use to read both type of excel’s including the format as xls and xlsx.

    Regards,

    Akshay

    • http://www.myjeeva.com Jeevanandam Madanagopal

      Hello Akshay –

      It’s not feasible with unified code, since the file formats are fundamentally different. You’ll have to write code using the HSSF package to read XLS files and XSSF package to read XLSX.

      Cheers,
      Jeeva

  • Sujith krishnan

    Hi Jeeva,
    how i can get sheet name using poi event model

  • Ramya A

    Hi Jeeva,

    Is it possible to get a specific row record without looping thru entire rows?

  • http://myjeeva.com/ Jeevanandam M.

    @ramyaanand:disqus just to clarify, Let’s say sheet has 20 rows and you want to get 12th row alone from sheet using excelReader library or POI library directly?

    Cheers,
    Jeeva

    • Ramya A

      Yes, using the excelReader or Using XSSF Event model. Thanks.

      • http://myjeeva.com/ Jeevanandam M.

        @ramyaanand:disqus Answer for POI library is no. Create an issue here https://github.com/jeevatkm/excelReader/issues I will create API for that in v1.3.

        Note: excelReader internally uses POI library.

  • Carlos De La Vega König

    Hi Jeeva,
    Because I get GRAVE: Unknown property ‘Name’ if my class property exists, getter and setter, and my cellMapping is correct.
    Grettings