Overview
Generating Excel files within an Android application enables structured data export, reporting, sharing, and interoperability with desktop tools like Microsoft Excel or Google Sheets. Android does not provide native APIs for Excel, so developers rely on third-party libraries to create, modify, and write Excel (.xls or .xlsx) files efficiently.
This document explains how to integrate a suitable library, structure data, generate workbooks, and write Excel files to storage in a way that is performant and compatible across Android devices.
Choosing the Right Library
There are two popular library families for working with Excel formats:
| Format | Library | Notes |
|---|---|---|
| .xls (older) | Apache POI HSSF | Full Excel-97 support, heavier |
| .xlsx (modern) | Apache POI XSSF, Android-optimized forks | Supports OOXML, richer formatting |
Standard Apache POI doesn’t work out-of-the-box on Android due to large dependencies and Java runtime differences. Use Android-optimized forks such as:
org.apache.poi:poi+ minimized builds- Third-party adjusted builds (e.g., Apache POI for Android)
For modern requirements, generating .xlsx files is recommended for better compatibility and extensibility.
Add Dependency
Using an Android-friendly POI build:
implementation 'org.apache.poi:poi:5.2.3'
implementation 'org.apache.poi:poi-ooxml:5.2.3'
If this causes method count or build size issues, consider ProGuard/R8 rules to shrink unused classes.
Basic Workbook and Sheet Creation
Excel files in POI are structured as:
- Workbook: Container for sheets
- Sheet: A grid of rows and cells
- Row: A line of cells
- Cell: A single data unit (text, numeric)
Example: Creating a Workbook
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Report");
Populating Data
Populate cells within rows using standard POI APIs:
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Score");
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue(101);
dataRow.createCell(1).setCellValue("Alice");
dataRow.createCell(2).setCellValue(89);
POI supports multiple data types (strings, numbers, booleans, formulas).
Adjusting Column Widths
To improve readability, auto-size columns after writing data:
for (int col = 0; col < 3; col++) {
sheet.autoSizeColumn(col);
}
Writing the Excel File to Storage
Android’s scoped storage rules differ by API level. Use getExternalFilesDir() or SAF (Storage Access Framework) for public storage.
Write to App-Specific External Directory
File file = new File(context.getExternalFilesDir(null), "Report.xlsx");
try (FileOutputStream fos = new FileOutputStream(file)) {
workbook.write(fos);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
This stores the file in your app’s external directory accessible without runtime permissions on Android 11+.
Requesting Write Permissions (Scoped Storage Context)
For public directories (Downloads / Documents), use Storage Access Framework:
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
intent.putExtra(Intent.EXTRA_TITLE, "Report.xlsx");
startActivityForResult(intent, CREATE_EXCEL_FILE_REQUEST);
In onActivityResult:
try (OutputStream os = getContentResolver().openOutputStream(uri)) {
workbook.write(os);
workbook.close();
}
This approach aligns with Android 10+ scoped storage policies.
Formatting Cells (Optional)
Excel allows cell styling for enhanced presentation:
CellStyle headerStyle = workbook.createCellStyle();
Font boldFont = workbook.createFont();
boldFont.setBold(true);
headerStyle.setFont(boldFont);
header.getCell(0).setCellStyle(headerStyle);
header.getCell(1).setCellStyle(headerStyle);
header.getCell(2).setCellStyle(headerStyle);
You can define borders, background colors, number formats, and alignment.
Export Use Cases
Common scenarios where Excel export is useful:
- User reports: Export scores, logs, or summaries.
- Data sharing: Allow cross-platform data exchange with desktop tools.
- Analytics: Save structured data for offline analysis.
- Audit trails: Track events or transactions with timestamped Excel logs.
Design the export layout and metadata (headers, footers, timestamps) according to your application context.
Performance Considerations
- Large datasets can increase memory usage. Consider batching or streaming where possible.
- POI builds are heavy; shrink unused classes via R8 or include only necessary modules.
- Use background threads for file generation to avoid UI blockage.
Example with Executors:
Executors.newSingleThreadExecutor().execute(() -> {
// Create and write Excel file
});
Testing and Validation
- Verify generated files on multiple devices and Excel clients.
- Confirm that special characters, encodings, and international text display correctly.
- Test save locations across Android versions (Scoped Storage on Android 11+).


