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.
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();
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With