Wednesday, October 28, 2015

Generate CSV in Playframwork

Example of generate CSV by modify output header.

  def exportCSV = Action {
    val data = "data 1, data 2"

      Ok(data).withHeaders(
          CONTENT_TYPE -> "text/csv",
          CONTENT_DISPOSITION -> "attachment; filename=foo.csv"
      )
  }

This method only suitable if you have small amount of data. If not, I would recommend using streaming.