Creating/Reading an Excel in Android.

Ranit Raj Ganguly
Apr 17 · 6 min read
Photo by William Iven on Unsplash

With the exponential increase in mobile devices, the ease of data accessibility has increased tenfold.

We can see all the data relevant to any domain on the mobile devices, for instance, the annual turnover of an organization, the number of employees who joined last month, etc. However, displaying the data on a mobile application is useful, although it becomes quite useless when we need to share the relevant data with the management or others. It is where exporting of data comes in handy and can be seen in most mobile applications today.

Overview:

We want to create a ‘Contact’ application of our own as shown in the video below.

In this application:

  • Initially, we will query the Contacts Content Provider in order to retrieve all the contacts present in the Android device.
Contacts Entrepot Android application preview

You can find the code for this application here.

However, we will only cover the following in this article:

  1. Downloading the jar file.

By the end of this article, you will be able to generate the below excel workbook:

Without wasting any time further, let us dive into it right away!

Downloading the JAR file

We will be making use of Apache’s POI project in order to achieve our goal. It is completely free and can be downloaded from here.

As per the official documentation:

You can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is your Java Excel solution (for Excel 97–2008).

So, not only can we use the POI for generating excel, it can be used for creating MS Word and MS Power point as well. However, it is beyond the scope of this article.

Exporting jar as a dependency in Android Studio

Next, we need to export the JAR as a dependency in Android Studio. I’ve depicted the same in a step-wise manner below:

Step 1: Navigate to the Project Structure on the left

Android file structure

Step 2: Switch to ‘Project’ form ‘Android’ by selecting the dropdown

Switching to Project view

Step 3: Select the ‘libs’ folder within the ‘app’ directory

Navigate to libs folder within the Project structure

Step 4: Copy the downloaded JAR and paste it into ‘libs’ directory

Copy and paste the JAR file into the libs directory

Step 5: (Most Important) Adding JAR as a library

Right click on the newly added JAR and select the option ‘Add as library’

Adding it as a library automatically handles adding and compiling the files (‘libs/library_name.jar’) in build.gradle. We don’t need to add it manually.

Thats it! You have now exported the JAR in Android Studio as a library.

Creating an Excel Workbook

In this section we will be covering a detailed walkthrough of the process of creating an excel workbook.

Before that, let’s familiarise ourselves with few excel terminologies:

  • Cell

We can try relating the terms in the following way:

  • Workbook is a collection of sheets.

So, let’s begin!

Step 1: Creating a new ‘Workbook’

Step 2: Creating a new ‘Sheet’

Step 3: Creating a new ‘Row’

Step 4: Creating a new ‘Cell’ and assigning ‘style’ to it

  • Initially, we declare a variable ‘cell’ of type ‘Cell’

Combining Steps 1 to 4:

The above code will generate a single row having 4 cells, which will look something like this:

Before we proceed with the next steps, make sure that you have added the necessary permissions in the ‘Manifest’

Importing data (Reading) from an Excel Workbook

In this section, we will try to understand the process of reading data from an excel workbook.

By the end of this section, we will be able to read data from the excel workbook displayed below i.e, the exported contact list from an Android device (and let’s say, display it in an android device).

Step 1: Access Excel file from storage

Step 2: Create an Instance having reference to .xls file

Step 3: Fetch sheet at desired index from workbook

Step 4: Iterate through rows and cells in the sheet

  • Iterate through each row in the sheet.

Combining Steps 1 to 4:

Exporting data (Writing) into an Excel Workbook

In this section, we will understand the mechanism to export data from an Android application (let’s say, the list of all contacts in a mobile phone) into an excel workbook and storing it in external storage.

Well, this is really simple since we already looked into the mechanism of generating a workbook before. All we need to do here is read data from any data source (eg: List) and cast it appropriately into cells and rows in order to generate the excel sheet shown below:

However, the complete code for writing data into an excel can be found at the end of this section.

Step 1: Fill data from list into Excel

Step 2: Check if external storage is read-only

Step 3: Check if storage is available

Step 4: Store Excel in External Storage

Combining all the steps:

While using the above function in your code (let’s say in MainActivity) just do the following:

And that’s how we generate an Excel using ‘Apache POI’ library. Also, read and write data from it.

If you’re interested to see the entire code in action, feel free to clone the repository from here.

Geek Culture

Proud to geek out.

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Ranit Raj Ganguly

Written by

Android Developer by profession | Game Developer by passion

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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