Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

JRExporterParameter is deprecated since jasper version 5.6

The current way since this version would be:

JRDocxExporter export = new JRDocxExporter();
export.setExporterInput(new SimpleExporterInput(jasperPrint));
export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File("path/toMy/report.docx")));

SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
//config.setFlexibleRowHeight(true); //Set desired configuration

export.setConfiguration(config);            
export.exportReport();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top