Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

322
Vistas
How to export Kotlin's data class list to CSV in android?

I am able to find method for exporting Java's list of POJO to csv but unable to find a method to export Kotlin's list of data class to CSV.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use the solution offered by this medium article:

To sum up

You can create a simple generic function

import java.io.FileWriter

inline fun <reified T> writeCsvFile(data: Collection<T>, fileName: String) {
    FileWriter(fileName).use { writer ->
        csvMapper.writer(csvMapper.schemaFor(T::class.java).withHeader())
                .writeValues(writer)
                .writeAll(data)
                .close()
    }
}

I tested it with the following dependencies:

com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.11.1
com.fasterxml.jackson.module:jackson-module-kotlin:2.12.5
over 4 years ago · Santiago Trujillo Denunciar

0

This can be achieved using basic Kotlin instead of an external library.

data class

data class Category(
    val id: String,
    val name: String,
)

utility function

fun <T> csvOf(
    headers: List<String>,
    data: List<T>,
    itemBuilder: (T) -> List<String>
) = buildString {
    append(headers.joinToString(",") { "\"$it\"" })
    append("\n")
    data.forEach { item ->
        append(itemBuilder(item).joinToString(",") { "\"$it\"" })
        append("\n")
    }
}

usage

fun main() {
    val firstCategory = Category(0, "Phone")
    val secondCategory = Category(1, "Laptop")
    val categories = listOf(firstCategory, secondCategory)
    val csv = csvOf(
        listOf("id", "name"),
        categories
    ) {
        listOf(it.id.toString(), it.name)
    }
    println(csv)
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda