Export Data Into The CSV File in Spring Boot

In this example, we can export data into the CSV file in Spring Boot Web Application. To achieve this task we can use the Apache Commons CSV library. We will create a Spring Boot application using Spring Web, Maven, Spring Data JPA, and H2 database.


Configuring Apache Common CSV library

Configuring this dependency in the pom.xml for export data into the CSV file in Spring Boot Application.


Overview of Export data into the CSV file in Spring Boot Web Application

In this example of the application, we can create one table on the database and insert some records on that table i.e., student. We will create one Java Bean class i.e., Student.java which is annotated with @Entity annotation to create a table on the database. We will create one repository interface for the persistence operation of that table i.e.,StudentRepoCSV.java. We will create a service interface i.e., StudentService.java and create one class implementation class of this service interface i.e.,StudentServiceImpl.java.We will create one utility class for generating the CSV file i.e.,CsvFileGenerator.java which contains code for creating the CSV file and writing data that will be getting from the database’s table. We will create one controller for generating a CSV file of the database table’s records i.e.,StudentController.java with a mapping URL that converts data into a CSV file. In this controller class, we will auto wiring service interface and CSV generating utility class. We will call the service’s method that will get all records of the database table. We will call the CSV file generating utility class’s method for writing the record that we will get from the service into the CSV file. We will one view page i.e., index.html.Then we will export that record into the CSV file

Table of content

     1. Creating Spring Boot Project
     2. Keep Eclipse IDE ready
     3. Defining Database 
     4. Creating a JPA Entity class
     5. Creating a JPA Repository
     6. Creating a Service
     7.  Creating a CSV File Generator class
     8.  Creating a Controller class
     9.  Creating a view page 
    10. Inserting Records
    11. Run the Spring Boot Project

1. Creating Spring Boot Project

We are creating a Spring Boot Project from this Spring Initializr.
Select the following dependencies:
   • Spring Web
   • Spring Data JPA 
   • H2 Database 

2.  Keep Eclipse IDE ready

Importing the spring boot project which you created for this export data into the CSV file in Spring Boot Web Application.

Project Structure of Export CSV Spring Boot


Maven Dependency

Here is the complete pom.xml file for this export data into the CSV file in Spring Boot Web Application.

<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath />
		<!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>

3. Defining Database 

We are configuring the H2 database configuration, embedded Tomcat Server port number and context path of this export data into the CSV file in Spring Boot in the application.properties file.

#H2 Database
spring.jpa.hibernate.ddl-auto= update
server.servlet.context-path= /demo

4. Creating a JPA Entity class

We are creating a JPA entity class to create a student table in the H2 database.

package com.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


public class Student {

  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  private String studentName;
  private String email;
  private String mobileNo;

  public Student() {

  public long getId() {
    return id;

  public void setId(long id) {
    this.id = id;

  public String getStudentName() {
    return studentName;

  public void setStudentName(String studentName) {
    this.studentName = studentName;

  public String getEmail() {
    return email;

  public void setEmail(String email) {
    this.email = email;

  public String getMobileNo() {
    return mobileNo;

  public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;

5. Create a JPA Repository

We are creating a JPA Repository to perform the persistence operation of the student table. The JpaRepository is available in the spring data jpa.

package com.example.repo;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.entity.Student;
public interface StudentRepoCSV extends JpaRepository < Student, Long > {

6. Creating a Service

We are creating a StudentService interface for this project.

package com.example.service;

import java.util.List;
import com.example.entity.Student;

public interface StudentService {
  void addStudent(Student student);
  List < Student > getStudentOfList();

We are creating an implementation class for the StudentService and this class is annotated with @Service annotation to make a service class.


package com.example.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entity.Student;
import com.example.repo.StudentRepoCSV;

public class StudentServiceImpl implements StudentService {

  StudentRepoCSV studentRepo;

  public void addStudent(Student student) {

  public List <Student> getStudentOfList() {
    return studentRepo.findAll();

7. Creating a CSV File Generator class

We are creating a class that generates a CSV file that uses a class from the library Apache Commons CSV library that helps to export data into CSV file in the Spring Boot of this web application.

package com.example.util;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.stereotype.Component;
import com.example.entity.Student;

public class CsvFileGenerator {
public void writeStudentsToCsv(List<Student> students, Writer writer) {
try {

CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT);
for (Student student : students) {
printer.printRecord(student.getId(), student.getStudentName(), student.getEmail(),
} catch (IOException e) {

8. Creating a Controller class

We are creating a controller class in which to create the request URL of this project.

package com.example.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import com.example.service.StudentService;
import com.example.util.CsvFileGenerator;

public class StudentController {

private StudentService studentService;

private CsvFileGenerator csvGenerator;

  public void exportIntoCSV(HttpServletResponse response) throws IOException {
    response.addHeader("Content-Disposition", "attachment; filename=\"student.csv\"");
    csvGenerator.writeStudentsToCsv(studentService.getStudentOfList(), response.getWriter());

9. Creating a view page

We are creating a view page under the static folder in this project.

<!DOCTYPE html>

<title>Export CSV File</title>
<body class="container">
<p>Click the button below to export the CSV file.</p>
<a href="http://localhost:8888/demo/export-to-csv" class="btn btn-primary">Download CSV File</a>

10. Inserting Records

We are inserting records on the student table with the help of CommandLineRunner. CommandLineRunner is an Interface which is having run() method. It is executed after the application startup.

package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.entity.Student;
import com.example.service.StudentService;

public class ExportCSVSpringBootExampleApplication implements CommandLineRunner {

  private StudentService studentService;
  public static void main(String[] args) {
    SpringApplication.run(ExportCSVSpringBootExampleApplication.class, args);

  public void run(String...a) {
    for (int i = 0; i <= 10; i++) {
      Student student = new Student();
      student.setStudentName("Student Name");

11. Run the Spring Project

Right-click on SpringBootApplication class(ExportCSVSpringBootExampleApplication) then click on Run As Java Application. Verify the database through the console. We can type this URL on the browser “http://localhost:8888/demo/h2-console/” to test whether the table is created on the database and data is inserted properly.


We can type this URL “http://localhost:8888/demo/” on the browser to check whether this application exports data to a CSV file properly or not.


Click on the Download CSV File link for downloading the CSV file.

1,Student Name,student@mail.com,XXXXXXXXXX

2,Student Name,student@mail.com,XXXXXXXXXX

3,Student Name,student@mail.com,XXXXXXXXXX

4,Student Name,student@mail.com,XXXXXXXXXX

5,Student Name,student@mail.com,XXXXXXXXXX

6,Student Name,student@mail.com,XXXXXXXXXX

7,Student Name,student@mail.com,XXXXXXXXXX

8,Student Name,student@mail.com,XXXXXXXXXX

9,Student Name,student@mail.com,XXXXXXXXXX

10,Student Name,student@mail.com,XXXXXXXXXX

11,Student Name,student@mail.com,XXXXXXXXXX

If you want to know how to import a CSV file in Spring Boot.


This example is explained:
   • How to transform data into a CSV file from a database?
   • How to use the Apache Common CSV library?

Leave a Comment