DEV Community

Discussion on: JXLS: How to generate Excel documents using templates?

Collapse
 
drvicx profile image
Victor Nuzhdin • Edited

Hi, everyone. Please help me with some problem. I need solution for catch and convert final output stream produced by "report.createDocument(outStream, templateName, data)" to HttpServletResponse in my Spring MVC Controller. Does anyone have a solution for intercepting the output stream? Thanks to Andres Sacco for such useful tool like a JXLS and thank to all for answers :)

Collapse
 
drvicx profile image
Victor Nuzhdin • Edited

I found solution. Maybe useful for someone
//--step1: create intermediate stream
ByteArrayOutputStream jxlsOutStream = new ByteArrayOutputStream();
//--step2: call create Excel stream method and write result to jxlsOutStream
report.createDocument(jxlsOutStream, templateName, data);
//--step3: create HTTPServletResponse output stream
ServletOutputStream outputStream = response.getOutputStream();
//--step4: convert ByteArrayOutputStream to InputStream
byte[] bytes = jxlsOutStream.toByteArray();
InputStream inputStream = new ByteArrayInputStream(bytes);
//--step5: copy data from InputStream to OutputStream data types
IOUtils.copy(inputStream, outputStream);
//--step6: now we have JXLS streaming data in HttpServletResponse
public void getJXLSStream(HttpServletResponse response) {..}

..thanks to Me))