download csv file spring boot

Solutions on MaxInterview for download csv file spring boot by the best coders in the world

showing results for - "download csv file spring boot"
Emily
14 Apr 2017
1package net.codejava;
2 
3import java.io.IOException;
4import java.text.DateFormat;
5import java.text.SimpleDateFormat;
6import java.util.Date;
7import java.util.List;
8 
9import javax.servlet.http.HttpServletResponse;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.stereotype.Controller;
12import org.springframework.web.bind.annotation.GetMapping;
13 
14import org.supercsv.io.CsvBeanWriter;
15import org.supercsv.io.ICsvBeanWriter;
16import org.supercsv.prefs.CsvPreference;
17 
18@Controller
19public class UserController {
20 
21    @Autowired
22    private UserServices service;
23     
24     
25    @GetMapping("/users/export")
26    public void exportToCSV(HttpServletResponse response) throws IOException {
27        response.setContentType("text/csv");
28        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
29        String currentDateTime = dateFormatter.format(new Date());
30         
31        String headerKey = "Content-Disposition";
32        String headerValue = "attachment; filename=users_" + currentDateTime + ".csv";
33        response.setHeader(headerKey, headerValue);
34         
35        /*This is where you receive the data which you wanna export to csv*/ 
36        List<User> listUsers = service.listAll();
37 
38        ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE);
39        String[] csvHeader = {"User ID", "E-mail", "Full Name", "Roles", "Enabled"};
40        String[] nameMapping = {"id", "email", "fullName", "roles", "enabled"};
41         
42        csvWriter.writeHeader(csvHeader);
43         
44        for (User user : listUsers) {
45            csvWriter.write(user, nameMapping);
46        }
47         
48        csvWriter.close();
49         
50    }
51     
52}
queries leading to this page
download csv files spring boot apihow change the data of csv file using the spring bootspring boot response csv filespring boot csv readspring boot generate csv fileshow to create a csv file through springboot java read csv file using spring boot batchread 2f write csv files in spring boot using apache commons csvspring boot example on working with csv filesspring boot csv file readspring boot read csv filedownload csv spring bootspring boot read csv file from resourcesdownload csv file spring bootjava spring boot read csv filespring boot csv file upload examplecreate a csv file in spring boothow to import csv file in spring boot spring suithow to read csv file in java spring bootcsv in spring bootspring boot export csv fileparsing csv file in spring bootcsv file upload spring bootcsv file to objects in spring bootcsv spring bootprocessing csv file in spring bootspring boot csv filespring api csv downloadcsv file in springbootspring boot reading csv file from locationread csv spring bootspring boot import csvdownload a csv file in java spring bootspring boot download csv exampleupload csv file in spring bootspring boot download csv file exampledownload csv file springspring boot read from csv filecsv file in spring bootspring boot load csv filehow to create a csv file through springboot java for my modelget csv file in request spring bootsave file from csv file spring bootworking with csv file in spring boot exampledownload csv file in java spring bootread data from csv file in spring bootspring boot export csv file examplecsv file where should be store in the spring bootspring boot import csvcreate csv spring bootgspring boot rest api to download csv filedownload file csv springhow to export csv file in spring bootread csv file from resources spring bootspring boot working witha csv filecsv files springhow to read csv file using spring bootread csv in spring boothow to read csv file in spring bootdownload csv file from rest call spring bootspring boot example with csv filesread csv file spring boothow to create csv file using spring boot and rest apijava spring boot download csv file with selectupload csv file spring bootextractioning file from csv java spring bootdownload csv file spring boot