Reputation: 543
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.
Upvotes: 4
Views: 24858
Reputation: 995
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();
Upvotes: 4
Reputation: 165
Based on JRDocxExporter, you can extend AbstractJasperReportsSingleFormatView :
public class CustomJasperWordReportsSingleFormatView extends AbstractJasperReportsSingleFormatView {
@Override
protected void renderReport(JasperPrint populatedReport, Map<String, Object> model, HttpServletResponse response) throws Exception {
super.renderReport(populatedReport, model, response);
}
@Override
protected JRExporter createExporter() {
return new JRDocxExporter();
}
@Override
protected boolean useWriter() {
// TODO Auto-generated method stub
return false;
}
}
and in your @Controller
you wrap the response with HttpServletResponseWrapper to get the byte array generated by the jasper report:
public byte[] getByteArrayWordReport(String jasperViewName, ModelMap model, HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws Exception{
View view=xmlViewResolver.resolveViewName(jasperViewName, null);
if(view==null){
log.error("Report not found:"+jasperViewName);
}
final ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
HttpServletResponseWrapper httpServletResponseWrapper=new HttpServletResponseWrapper(httpServletResponse){
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(byteArrayOutputStream);
}
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
byteArrayOutputStream.write(b);
}
};
}
@Override
public void addCookie(Cookie cookie) {
}
@Override
public void addDateHeader(String name, long date) {
}
@Override
public void setHeader(String name, String value) {
}
@Override
public void addHeader(String name, String value) {
}
@Override
public void setIntHeader(String name, int value) {
}
@Override
public void addIntHeader(String name, int value) {
}
@Override
public void setContentLength(int len) {
}
@Override
public void setContentType(String type) {
}
};
view.render(model, httpServletRequest, httpServletResponseWrapper);
return byteArrayOutputStream.toByteArray();
}
After that you have the byte array representing the Word Document.
Upvotes: 3
Reputation: 14202
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();
}
Upvotes: 4