如何从Spring Controller中返回浏览器中的CSV数据

时间:2014-04-08 20:42:42

标签: java spring spring-mvc csv

假设我在字符串中有CSV数据并希望从Spring控制器返回它。想象一下数据看起来像这样

a,b,c 
1,2,3
4,5,6

无论我尝试过什么,换行符都是字面上的' \ n'在响应内容中,如果我在" \ n"中双重转义它们,响应也只包含双反斜杠。一般来说,如何在不修改换行符的情况下返回带有换行符的纯文本数据?我知道如何返回纯文本,但是,内容仍然带有转义换行符......这就是我目前所拥有的(使用Spring 3.0.5,而不是选择)

@RequestMapping(value = "/api/foo.csv")
public ResponseEntity<String> fooAsCSV() {

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "text/plain; charset=utf-8");

    String data = "a,b,c\n1,2,3\n3,4,5";
    return new ResponseEntity<>(data, responseHeaders, HttpStatus.OK);
}

从字面上产生字符串

"a,b,c\n1,2,3\n,3,4,5"

在浏览器中。如上所示,如何使用新线路生成正确的数据?

5 个答案:

答案 0 :(得分:17)

您可以使用例如

直接写入回复
@RequestMapping(value = "/api/foo.csv")
public void fooAsCSV(HttpServletResponse response) {         
    response.setContentType("text/plain; charset=utf-8");
    response.getWriter().print("a,b,c\n1,2,3\n3,4,5");
}

由于返回类型为voidHttpServletResponse被声明为方法参数,因此假定在此方法返回时完成请求。

答案 1 :(得分:5)

您是否在控制器方法上尝试了@ResponseBody

@RequestMapping(value = "/api/foo.csv")
@ResponseBody
public String fooAsCSV(HttpServletResponse response) {         
    response.setContentType("text/plain; charset=utf-8");
    String data = "a,b,c\n1,2,3\n3,4,5";
    return data;
}

编辑:Spring文档在此解释:http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

答案 2 :(得分:1)

您可以使用库 supercsv

<dependency>
  <groupId>net.sf.supercsv</groupId>
  <artifactId>super-csv</artifactId>
  <version>2.1.0</version>
</dependency>

这里是使用方法:

1-定义要作为csv编写的模型类:

public class Book {
private String title;
private String description;
private String author;
private String publisher;
private String isbn;
private String publishedDate;
private float price;

public Book() {
}

public Book(String title, String description, String author, String publisher,
        String isbn, String publishedDate, float price) {
    this.title = title;
    this.description = description;
    this.author = author;
    this.publisher = publisher;
    this.isbn = isbn;
    this.publishedDate = publishedDate;
    this.price = price;
}

// getters and setters...
}

2-做以下魔术:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

/**
 * This Spring controller class implements a CSV file download functionality.
 *
 */
@Controller
public class CSVFileDownloadController {
    @RequestMapping(value = "/downloadCSV")
    public void downloadCSV(HttpServletResponse response) throws IOException {

        String csvFileName = "books.csv";

        response.setContentType("text/csv");

        // creates mock data
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"",
            csvFileName);
        response.setHeader(headerKey, headerValue);

        Book book1 = new Book("Effective Java", "Java Best Practices",
            "Joshua Bloch", "Addision-Wesley", "0321356683", "05/08/2008",
            38);

        Book book2 = new Book("Head First Java", "Java for Beginners",
            "Kathy Sierra & Bert Bates", "O'Reilly Media", "0321356683",
            "02/09/2005", 30);

        Book book3 = new Book("Thinking in Java", "Java Core In-depth",
            "Bruce Eckel", "Prentice Hall", "0131872486", "02/26/2006", 45);

        Book book4 = new Book("Java Generics and Collections",
            "Comprehensive guide to generics and collections",
            "Naftalin & Philip Wadler", "O'Reilly Media", "0596527756",
            "10/24/2006", 27);

        List<Book> listBooks = Arrays.asList(book1, book2, book3, book4);

        // uses the Super CSV API to generate CSV data from the model data
        ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(),
            CsvPreference.STANDARD_PREFERENCE);

        String[] header = { "Title", "Description", "Author", "Publisher",
            "isbn", "PublishedDate", "Price" };

        csvWriter.writeHeader(header);

        for (Book aBook : listBooks) {
            csvWriter.write(aBook, header);
        }

        csvWriter.close();
    }
}

答案 3 :(得分:1)

我最近有一个类似的任务。使用了 ResponseEntity 和响应头 Content-Disposition。像这样:

@RequestMapping(value = "/api/foo.csv")
public ResponseEntity<byte[]> fooAsCSV() {

  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.add("Content-Type", "application/vnd.ms-excel");
  responseHeaders.add("Content-Disposition", "attachment; filename=abc.csv");

  String data = "a,b,c\n1,2,3\n3,4,5";
  return new ResponseEntity<>(data.getBytes("ISO8859-15"), responseHeaders, HttpStatus.OK);
}

答案 4 :(得分:0)

这是Bart回答的更详细的示例:

@GetMapping(value = "csv")
public void exportCsv(HttpServletResponse response) {
    try {
        // prepare response encoding and Headers
        response.setContentType("text/csv");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Content-Disposition", "attachment; filename=a.csv"); // specify the real file name users will get when download
        try (Writer writer = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
            // writer.write('\uFEFF'); // BOM is essential in some scenary
            writer.write("key,value\n");
            List<Row> row = rowService.getAll();
            for (Column column : row) {
                writer.write(String.format("%d,%s)\n",column.getKey(), column.getValue()));
            }
            writer.flush(); // DONNOT forget to flush writer after everything done
        } // writer will be closed automatically
    } catch (IOException e) {
        log.error("failed to export csv", e);
    }
}