logo

Spring Boot Web Application Download CSV File

In this article we show you how to implement dowloading CSV file in Spring Boot Web Application.

Overview Project Structure

Throughout this article we implement the Spring Boot project as below structure Spring Boot Web Application Download CSV File Project Structure

Gradle Build Dependencies

Define below dependencies in build.gradle if you are using gradle.

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'

Maven Build Dependencies

Define this dependencies in pom.xml if you are using maven.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Creating model class to represent your data

In this example application, we download the list of Employees. So you need to implement Employee class to represent Employee data as below.

package simplesolution.dev.model;

public class Employee {
    private int employeeId;
    private String firstName;
    private String lastName;

    public Employee(int employeeId, String firstName, String lastName) {
        this.employeeId = employeeId;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Implement CSV Utilities class to download CSV file

package simplesolution.dev.utils;

import simplesolution.dev.model.Employee;

import java.io.PrintWriter;
import java.util.List;

public class CsvUtils {

    public static void downloadCsv(PrintWriter writer, List<Employee> employees) {
        writer.write("Employee ID, First Name, Last Name \n");
        for (Employee employee : employees) {
            writer.write(employee.getEmployeeId() + "," + employee.getFirstName() + "," + employee.getLastName() + "\n");
        }
    }

}

Implement Controller

package simplesolution.dev.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import simplesolution.dev.model.Employee;
import simplesolution.dev.utils.CsvUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DownloadController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/download/employee.csv")
    public void downloadCsv(HttpServletResponse response) throws IOException {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; file=employee.csv");
        CsvUtils.downloadCsv(response.getWriter(), createTestData()) ;
    }

    private List<Employee> createTestData() {
        List<Employee> data = new ArrayList<>();
        data.add(new Employee(123, "Buffet","Jimmy"));
        data.add(new Employee(456, "Cartman","Eric"));
        data.add(new Employee(789, "Jefferson","George"));
        return data;
    }

}
<html>
<head>
    <title>Download CSV File - simplesolution.dev</title>
</head>
<body>
<h1>Spring Boot Web Application Download CSV File</h1>
<a href="http://localhost:8080/download/employee.csv">Download employee.csv</a>
</body>
</html>

Run the application

Run the web application and access via browser you can see the download link as below. Spring Boot Web Application Download CSV File Run Application

Click on download link to download CSV file then open it you can see the result as below. Spring Boot Web Application Download CSV File Result

The source code in this article can be found at: https://github.com/simplesolutiondev/SpringBootDownloadCSV

Happy Coding 😊