Spring Boot CSV export — OpenCSV + Kotlin

1. Introduction

2. Imports

implementation("com.opencsv:opencsv:5.2")
implementation("org.springframework.boot:spring-boot-starter-web")

3. Create a User Class

class User(
val id: Long,
val email: String,
val name: String
)

4. Create a User Service

@Service
class UserService {
fun findAllUsers() =
listOf(
User(1, "piotr@codersee.com", "Piotr"),
User(2, "adam@codersee.com", "Adam"),
User(3, "john@codersee.com", "John"),
User(4, "karim@codersee.com", "Karim")
)
}

5. Create REST Controller

@RestController
class UserController(
private val csvService: CsvService
) {
@PostMapping("/api/user/csv")
fun getAllUsersCsvExport(response: HttpServletResponse) {
csvService.exportUserListToCsv(response.writer)
}

@PostMapping("/api/user/csv/string")
fun getAllUsersCsvExportWihStringWriter(): ResponseEntity {
val result = csvService.exportUserListToCsvWithStringWriter()

return ResponseEntity.ok(result.toString())
}
}

6. Create CSV Service

@Service
class CsvService(
private val userService: UserService
) {
private fun prepareMappingStrategy(): ColumnPositionMappingStrategy {
val columns = arrayOf("id", "email", "name")

val strategy = ColumnPositionMappingStrategy()
strategy.setColumnMapping(*columns)
strategy.type = User::class.java

return strategy
}
}

6.1. Export Beans With Default Values

fun exportUserListToCsv(writer: PrintWriter) {
val users = userService.findAllUsers()

val mappingStrategy = prepareMappingStrategy()
val bean = StatefulBeanToCsvBuilder(writer)
.withMappingStrategy(mappingStrategy)
.build()

bean.write(users)
}
"1","piotr@codersee.com","Piotr"
"2","adam@codersee.com","Adam"
"3","john@codersee.com","John"
"4","karim@codersee.com","Karim"

6.2. Export Beans Without the Quote Chars

fun exportUserListToCsvWithoutQuoteChar(writer: PrintWriter) {
val users = userService.findAllUsers()

val mappingStrategy = prepareMappingStrategy()
val bean = StatefulBeanToCsvBuilder(writer)
.withMappingStrategy(mappingStrategy)
.withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.build()

bean.write(users)
}
1,piotr@codersee.com,Piotr
2,adam@codersee.com,Adam
3,john@codersee.com,John
4,karim@codersee.com,Karim

6.3. Export Beans With Custom Separator

fun exportUserListToCsvCustomSeparator(writer: PrintWriter) {
val users = userService.findAllUsers()

val mappingStrategy = prepareMappingStrategy()
val bean = StatefulBeanToCsvBuilder(writer)
.withMappingStrategy(mappingStrategy)
.withSeparator(';')
.build()

bean.write(users)
}
"1";"piotr@codersee.com";"Piotr"
"2";"adam@codersee.com";"Adam"
"3";"john@codersee.com";"John"
"4";"karim@codersee.com";"Karim"

6.4. Export Beans With Custom Line Ending

fun exportUserListToCsvCustomLineEnd(writer: PrintWriter) {
val users = userService.findAllUsers()

val mappingStrategy = prepareMappingStrategy()
val bean = StatefulBeanToCsvBuilder(writer)
.withMappingStrategy(mappingStrategy)
.withLineEnd("||\n")
.build()

bean.write(users)
writer.close()
}
"1","piotr@codersee.com","Piotr"||
"2","adam@codersee.com","Adam"||
"3","john@codersee.com","John"||
"4","karim@codersee.com","Karim"||

6.5. Export Beans With StringWriter

fun exportUserListToCsvWithStringWriter(): StringWriter {
val users = userService.findAllUsers()

val mappingStrategy = prepareMappingStrategy()
val writer = StringWriter()
val bean = StatefulBeanToCsvBuilder(writer)
.withMappingStrategy(mappingStrategy)
.build()
bean.write(users)

writer.close()
return writer
}

7. Conclusion

Hi, my name is Piotr and I am the founder of Codersee- a technical blog, where I am teaching programming by practical step by step tutorials.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store