Implementing Data Export (CSV, Excel) from Mobile App
User clicks "Export" — and waits. If they have 5000 rows in local SQLite or Room database, and export happens on main thread, app freezes for three seconds, and on old devices ANR (Application Not Responding) won't wait. This is first and most common mistake in export implementation.
What Breaks Most Often in Practice
UI blocking during file generation. Serializing 10,000 rows to CSV — not "instant operation". On Android need CoroutineScope(Dispatchers.IO), on iOS — DispatchQueue.global(qos: .userInitiated). Always move file generation to background thread, return result via callback or Flow.
Encoding and delimiter. CSV — trap. Excel in Windows by default expects Windows-1252 encoding and ; delimiter, not ,. If you give UTF-8 without BOM — Cyrillic becomes mojibake in office client. Right CSV for Excel: UTF-8 with BOM (\uFEFF at start) and ; delimiter. Or directly export .xlsx via library.
Excel export (.xlsx). On Android use Apache POI or lighter FastExcel. On iOS — xlsxwriter via Swift Package or own XML generator (.xlsx — ZIP with XML files inside). For React Native — react-native-xlsx over js library xlsx.
How We Build Export
Simple scheme: read data from local database → transform to row model → write to file → share via system ShareSheet / Intent.ACTION_SEND.
On Android with Room:
viewModelScope.launch(Dispatchers.IO) {
val rows = database.transactionDao().getAll()
val file = CsvExporter.export(rows, context.cacheDir)
withContext(Dispatchers.Main) {
shareFile(file, "text/csv")
}
}
On iOS similarly via Task.detached:
Task.detached(priority: .userInitiated) {
let rows = await store.fetchAll()
let url = try CsvExporter.write(rows, to: .cachesDirectory)
await MainActor.run { presentShareSheet(url) }
}
For .xlsx on iOS generate XML structure manually or via CoreXLSX / xlsxwriter. For simple tables — XML approach faster and without dependencies.
Progress on Large Volume
If rows > 50,000 — show ProgressView with real percentage. On Android via StateFlow<Int> in ViewModel, on iOS via @Published var progress: Double. Write file in batches of 1000 rows, update counter after each batch.
File Format and Share
After generation, put file in cacheDir (Android) or FileManager.default.temporaryDirectory (iOS). Share via:
- Android:
FileProvider+Intent.ACTION_SENDwith correct MIME type (text/csvorapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet) - iOS:
UIActivityViewControllerwith[fileURL]
Don't save to Downloads without explicit user request — violates both platforms guidelines.
Work Scope
- Choose format (CSV / XLSX) and agree on column structure
- Background file generation without UI blocking
- Correct encoding and localized delimiters
- Progress indicator for large exports
- Share via system ShareSheet / Intent
- Tests on real data volumes
Timeline
Simple CSV export from ready database: 0.5–1 day. With format choice (CSV/XLSX), date range filters and progress: 1.5–2 days. Cost calculated after data structure analysis.







