Getting Started with Aspose.Cells for Android: Quick Setup & First Spreadsheet

Getting Started with Aspose.Cells for Android: Quick Setup & First Spreadsheet

This guide walks you through installing Aspose.Cells for Android, creating a simple spreadsheet, and saving it on an Android device. All code samples use Kotlin and assume Android Studio with a minimum SDK that matches Aspose.Cells requirements.

Prerequisites

  • Android Studio installed
  • A Kotlin-based Android project (Java samples shown where useful)
  • Internet access to download dependencies

1. Add Aspose.Cells for Android to your project

Aspose distributes libraries as AAR/JAR files or via Maven. The simplest approach is using Maven Central (if available for your Aspose version) or adding the AAR to your project.

Option A — Maven (preferred if available)

  1. Open module-level build.gradle (usually app/build.gradle).
  2. Add the Aspose repository and dependency (replace with the correct latest version):

gradle

repositories { mavenCentral() }

dependencies {

implementation "com.aspose:aspose-cells-android:23.9" 

}

Sync the project.

Option B — Manual AAR

  1. Download the Aspose.Cells for Android AAR from Aspose’s website.
  2. Copy the AAR into app/libs/.
  3. In build.gradle:

gradle

repositories { flatDir {

    dirs 'libs' } 

}

dependencies {

implementation(name:'aspose-cells-android-23.9', ext:'aar') 

}

Sync the project.

2. Required Android permissions

To write files to external storage, add permissions in AndroidManifest.xml (for API < 29):

xml

<uses-permission android:name=android.permission.WRITE_EXTERNALSTORAGE />

For API 29+, prefer scoped storage and use MediaStore or app-specific external directories. Request runtime permissions when needed.

3. Basic initialization

Aspose.Cells typically does not require complex initialization in app code. Ensure your app has access to the library and required permissions. If your Aspose package requires a license file, place the license in assets and load it early (optional):

kotlin

// Example: load license from assets (if you have one) val license = com.aspose.cells.License() assets.open(“Aspose.Cells.lic”).use { license.setLicense(it) }

4. Create your first spreadsheet (Kotlin)

This example creates a workbook, adds data, formats headers, and saves an XLSX file to app-specific external storage.

kotlin

import com.aspose.cells.Workbook import com.aspose.cells.Style import com.aspose.cells.StyleFlag import java.io.File fun createSampleSpreadsheet(context: android.content.Context) { // Create a new workbook and get the first worksheet val workbook = Workbook() val sheet = workbook.worksheets[0] val cells = sheet.cells // Add headers cells.get(“A1”).putValue(“ID”) cells.get(“B1”).putValue(“Name”) cells.get(“C1”).putValue(“Email”) // Add sample rows cells.get(“A2”).putValue(1) cells.get(“B2”).putValue(“Alice”) cells.get(“C2”).putValue([email protected]) cells.get(“A3”).putValue(2) cells.get(“B3”).putValue(“Bob”) cells.get(“C3”).putValue([email protected]) // Format header row: bold and background color val headerStyle = Style() headerStyle.font.isBold = true headerStyle.pattern = com.aspose.cells.BackgroundType.SOLID headerStyle.foregroundColor = android.graphics.Color.YELLOW val styleFlag = StyleFlag() styleFlag.cellShading = true styleFlag.font = true cells.rows[0].applyStyle(headerStyle, styleFlag) // Autosize columns sheet.autoFitColumns() // Save to app-specific external files directory val outDir = context.getExternalFilesDir(null) val outFile = File(outDir, “sample_asposecells.xlsx”) workbook.save(outFile.absolutePath) }

Call createSampleSpreadsheet(this) from an Activity or a coroutine-aware scope (handle permissions where necessary). The saved file will be in your app’s external files folder and accessible via USB or Android Studio’s Device File Explorer.

5. Reading an existing spreadsheet

kotlin

import com.aspose.cells.Workbook import java.io.File fun readSpreadsheet(filePath: String): List<List<String>> { val workbook = Workbook(filePath) val sheet = workbook.worksheets[0] val cells = sheet.cells val rows = mutableListOf<List<String>>() for (r in 0 until sheet.cells.maxDataRow + 1) { val row = mutableListOf<String>() for (c in 0 until sheet.cells.maxDataColumn + 1) { val cell = cells.get(r, c) row.add(cell.stringValue ?: ””) } rows.add(row) } return rows }

6. Common tasks and API pointers

  • Formulas: cells.get(“D2”).putValue(“=SUM(A2:A10)”)
  • Charts: Use charts collection on worksheet.charts to add and configure charts.
  • Images: worksheet.pictures.add(…) to insert images.
  • Export to PDF: workbook.save(path, com.aspose

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *