Reading Microsoft Excel XLSX files in Java

In a previous tutorial, you learnt how to generate a Microsoft Excel XLSX files in Java. Today, you are going to learn how to read Microsoft Excel XLSX files in a Java application.

Image for post
Image for post

For that, we are going to use the great Apache POI API available just there :

Note that you can watch this tutorial as a video on YouTube :

Adding Apache POI as a dependency

First step is to add Apache POI as a Maven dependency in the build file of our Java project :

We have chosen to read Excel XLSX files in that tutorial but the logic is the same to read Excel XLS older file format. So, we have added both dependencies in our Maven POM but just the poi-ooxml is needed for our example.

Loading the file to read

First, we need to load Excel XLSX file to read. Our file will be named contacts.xlsx and it will contain the data written in our previous tutorial learning you how to generate Excel XLSX files in Java.

So, we create a File object with the name of the file in parameter and then, we create FileInputStream instance for this file. This FileInputStream instance will be passed in parameter of the XSSFWorkbook object during its creation. The XSSFWorbook is the root object modeling an Excel XLSX file in the Apache POI Library.

It gives us the following code to load the file :

Reading the data

Now, we are reading the data of the Excel XLSX file. First, we get the first sheet in the XSSFWorkbook by calling the getSheetAt method of the XSSFWorkbook instance created previously.

Then, we get an iterator to iterate on the rows of this sheet. Note that a sheet is represented by a XSSFSheet object in the API. To iterate on each rows of our sheet, we need to use the Iterator object we got like usual in Java.

In the same way, for each row, we are going to iterate on each cell. For that we call the cellIterator method of the current Row object instance. And we iterate on the Cell objects of the current Row instance. For each cell, we get the value as a String and we display it in the console.

Finally, we don’t forget to close the streams opened by calling the close methods of the XSSFWorkbook and FileInputStream objects.

It gives us the following complete code for our ReadExcel class :

Final Result

By executing the ReadExcel class on a JVM, the contacts.xlsx file should be read on your machine and you should have the following result :

Image for post
Image for post

That’s all for that tutorial.

If you want to learn how to generate Microsoft Excel XLSX files in Java, you can read our dedicated tutorial on Medium :

To discover more tutorials on Java and Android development, you can subscribe to the SSaurel’s Channel :

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store