How to generate, export to word docx file?

eeijlar picture eeijlar · Apr 9, 2014 · Viewed 13.4k times · Source

I am trying to generate a docx in jasper report. I have this code:

JRDocxExporter exporter = new JRDocxExporter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();    
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport(); 

How do I write the report out to file? Most of the examples I have seen are all around using servlets.

Answer

user432 picture user432 · Apr 10, 2014

Add the parameter JRExporterParameter.OUTPUT_FILE_NAME to specify the file and remove the parameter JRExporterParameter.OUTPUT_STREAM.

JRDocxExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "myreport.docx");
exporter.exportReport();