A Simple way to work with Excel in Android App

Part 1- Writing Excel Sheet

Hafiz Faraz Tariq
May 31 · 2 min read
Copied from More App

So, in today’s article you will learn writing of excel sheet in Android app with a very simple and easy way. To do this, we need to add libraries from Apache POI.

In App level gradle file, add below dependencies.

//Apache POI for excel
implementation 'com.github.SUPERCILEX.poi-android:poi:3.17'
compileOnly 'org.apache.poi:poi-ooxml:3.17'

Have previous knowledge about:
1- Kotlin
2- Save file in Android

Create Excel Workbook

To create excel file, we first need to create Workbook from XSSFWorkbook and then need to create sheet to add data into it.

You can create multiple sheets in a single workbook by using the createSheet method of workbook.

Create Sheet Header

In the first row of excel sheet, usually people create sheet header. So, below code will help you to create and understand header in excel sheet.

The maximum column width for an individual cell is 255 characters and it’s value in integer cannot be greater than 65280.

Header Style

In the above code snippet, you can assign any color to the cell background or Foreground. For more details, you can see the implementation of setFillPattern method in CellStyle class. You can also customize the font style of the text of a cell including color.

Add Data

To add data in the excel sheet, you need to create a row and need to add data to each cell of the row.

Create Cell

Create cell with row and column indexes and add value into it.

Create Excel File

Now it’s time to create excel file from workbook in Android Directory.

Hurry!! creating and writing excel file in Android App successfully done.

For reading excel sheets please check Part-2. Next parts of updating excel sheet will be published soon. Please follow for more posts and different solutions to the problems in Android App.

Geek Culture

Proud to geek out. Follow to join our +1M monthly readers.