My Development Blog

A Summary of my hacks and learnings.

Download a Csv in Your Play! Framework Application

In this post, I’ll share a simple way to download CSV files in Play Framework. This is especially useful, if you want to export out some reports or analytics data for offline use. So let’s get started.

  1. First create a new Play! application (I’m skipping all this for the sake of sticking to the point)
  2. Have a seperate controller/method which handles the generation of CSV files.
  3. In that controller add the following code:
CSV download snippet
1
2
3
4
5
6
7
8
9
response.contentType = "text/csv";

response.setHeader("Content-Disposition", "attachment;filename="" + <filename> +".csv" + """);

response.writeChunk(<header>); 

response.writeChunk("\n"); 

response.writeChunk(<content>);

Note: Remember to separate your content with a “,” so that it will be columnised in the file