Top Excel Libraries for Android

You know, working with Excel files in Android apps can be quite a pickle. There are some nifty libraries out there that can make this process as smooth as butter. Let's look at some of the best Excel libraries for Kotlin on Android and see how they can save us from pulling our hair out.

1. Apache POI

Apache POI is like the Swiss Army knife for handling Microsoft Office documents. It's been around for a while and is well-known in the Java world, which means it's also usable in Kotlin for Android.

  • Read and write Excel files (.xls and .xlsx).
  • Manipulate sheets, cells, and formulas.
  • Handle styles and formatting.
  • Work with charts and images within Excel files.

Installation:.

Add the following to your build.gradle (Module: app) file:.

dependencies {
    implementation 'org.apache.poi:poi:5.2.3'
    implementation 'org.apache.poi:poi-ooxml:5.2.3'
}

Here's a snippet to create a new Excel file:.

import org.apache.poi.xssf.usermodel.XSSFWorkbook

val workbook = XSSFWorkbook()
val sheet = workbook.createSheet("Sheet1")

val row = sheet.createRow(0)
val cell = row.createCell(0)
cell.setCellValue("Hello Excel!")

val fileOut = FileOutputStream("example.xlsx")
workbook.write(fileOut)
fileOut.close()

This code creates a workbook, a sheet, a row, and a cell, then writes "Hello Excel!" into the cell. It finally saves the file as example.xlsx.

Comparison:.

Apache POI is robust and feature-rich but can be a bit heavy for mobile applications. It's great for complex tasks but might be overkill for simple Excel operations.

Reference Link:.

Apache POI

2. JXL (JExcelAPI)

JXL is another library that's been around the block. It's specifically for working with Excel files, particularly the older .xls format.

  • Read, write, and modify .xls files.
  • Supports formulas, fonts, and colors.
  • Handles multiple sheets.

Installation:.

Add this to your build.gradle (Module: app):.

dependencies {
    implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
}

Here's how you can read an Excel file:.

import jxl.*
import jxl.read.biff.BiffException

try {
    val workbook = Workbook.getWorkbook(File("example.xls"))
    val sheet = workbook.getSheet(0)
    val cell = sheet.getCell(0, 0)
    println(cell.contents)
    workbook.close()
} catch (e: IOException) {
    e.printStackTrace()
} catch (e: BiffException) {
    e.printStackTrace()
}

This code opens an Excel file, reads the content of the first cell, and prints it out.

Comparison:.

JXL is straightforward and easy to use, but it doesn't support the newer .xlsx format. It's a good choice if you're only dealing with .xls files.

Reference Link:.

JXL (JExcelAPI)

3. SimpleExcel

SimpleExcel is a Kotlin library that aims to provide a simple and idiomatic way to handle Excel files in Android.

  • Read and write both .xls and .xlsx files.
  • Kotlin-first design, making it more idiomatic for Kotlin developers.
  • Lightweight and easy to use.

Installation:.

Include this in your build.gradle (Module: app):.

dependencies {
    implementation 'com.github.wimasm:SimpleExcel:0.3.0'
}

Here's a Kotlin script to write data to an Excel file:.

import com.github.wimasm.simpleexcel.SimpleExcelWriter
import com.github.wimasm.simpleexcel.SimpleExcelWriterSheet
import java.io.FileOutputStream

val sheet = SimpleExcelWriterSheet("Sheet1")
sheet.writeRow { "Name", "Age" }
sheet.writeRow { "Alice", 25 }

val workbook = SimpleExcelWriter.buildWorkbook {
    addSheet(sheet)
}

val fileOut = FileOutputStream("simpleexample.xlsx")
workbook.write(fileOut)
fileOut.close()

This snippet creates a workbook with a sheet, writes a header row and a data row, and saves it as simpleexample.xlsx.

Comparison:.

SimpleExcel is designed with Kotlin in mind, making it a breeze to work with for Kotlin developers. It's a newer library, so it might not be as battle-tested as Apache POI or JXL.

Reference Link:.

SimpleExcel