javajasper-reportsdocxexport-to-word

How to generate, export to word docx file?


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.


Solution

  • 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();