Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating the report in word format using jasper

I am using jasper-reports 4.5.0,spring 3.0.5RELEASE integration to generate the reports.Till now i generated in html,csv,pdf formats.But my client wants the report in word format also(.doc format).How can i generate the report in this .doc format.

like image 674
user1434746 Avatar asked Dec 27 '22 23:12

user1434746


2 Answers

For future readers setParameter is deprecated and you should use exporters like this:

JasperReports 6.1.0

import java.io.File;

//import net.sf.jasperreports.engine.export.JRRtfExporter;
//import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;

File sourceFile = new File("*.jasper");

JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);

File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".docx|.rtf");

//JRDocxExporter exporter = new JRDocxExporter();
//JRRtfExporter exporter = new JRRtfExporter();

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));

exporter.exportReport();
like image 149
Ako Avatar answered Dec 31 '22 11:12

Ako


Just to provide an example code based on Alex's suggestion:

To use JRRtfExporter:

protected byte[] exportReportToRtf(JasperPrint jasperPrint) throws JRException{
   JRRtfExporter exporter = new JRRtfExporter();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();    
   exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
   exporter.exportReport(); 
   return baos.toByteArray();
}

Similarly to us JRDocxExporter:

protected byte[] exportReportToRtf(JasperPrint jasperPrint) throws JRException{
   JRDocxExporter exporter = new JRDocxExporter();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();    
   exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
   exporter.exportReport(); 
   return baos.toByteArray();
}

Edit based on comment:

Here is the list of the JaperReport Views that the Sprig Fraework provides.

They do not seem to have one specifically for the doc format. You will probably have write your own by extending AbstractJasperReportsSingleFormatView. It seems you would only need to implement the createExporter() method.

protected JRExporter createExporter(){
     return new JRDocxExporter();
}
like image 36
Jacob Schoen Avatar answered Dec 31 '22 12:12

Jacob Schoen