I have the following code in Play for Scala that attempts to send an Excel file to the browser:
val out = new ByteArrayOutputStream
val exporter = new org.pivot4j.ui.poi.ExcelExporter(out)
val renderer = new TableRenderer
renderer.render(model, exporter);
out.flush
out.close
Ok(out).withHeaders(
CONTENT_TYPE -> "application/vnd.ms-excel",
CONTENT_DISPOSITION -> s"attachment; filename = file.xlsx")
The Ok
throws a compilation error:
◾Cannot write an instance of java.io.ByteArrayOutputStream to HTTP response. Try to define a Writeable[java.io.ByteArrayOutputStream]
Even though ByteArrayOutputStream
is of type output, it cannot be written. How to fix this?
Try calling toByteArray
on out
like so:
Ok(out.toByteArray).withHeaders(...)
DefaultWritables provides a list of Writable
provided by Play out-of-the-box. In particular, there exists Writeable[Array[Byte]] which should get picked up when you call toByteArray