[play 2.3 java] How to serve a generated Apache POI excel file without creating a file on the server?

246 views
Skip to first unread message

Ben Developer

unread,
Jul 2, 2014, 12:19:05 PM7/2/14
to play-fr...@googlegroups.com
Hello,

I'm creating an Excel file on-the-fly using Apache POI. 

I want Play to send the file content in an Excel file, but without saving the file to the server. 

Is it correct that I would need to use Chunked Responses? 

I'm using Play 2.3 Java.

Thanks!

Ramazan AYYILDIZ

unread,
Jun 18, 2015, 4:38:49 AM6/18/15
to play-fr...@googlegroups.com
You can use ByteArrayOutputStream and you can serve it chunked data as below:


val responseStream: java.io.ByteArrayOutputStream = new ByteArrayOutputStream()

val sheetOne = Workbook {
  ...
}
val workbookImpure = new WorkbookImpure(sheetOne)
workbookImpure.saveToStream(responseStream)


Ok.chunked(
Enumerator(responseStream.toByteArray).andThen(Enumerator.eof)
).as(
"application/vnd.ms-excel").withHeaders("Content-Disposition" -> s"attachment; filename=filename.xls")
Reply all
Reply to author
Forward
0 new messages