使用jasper以word格式生成报告

时间:2012-06-07 07:30:43

标签: java spring jasper-reports

我正在使用jasper-reports 4.5.0,spring 3.0.5RELEASE集成来生成报告。现在我用html,csv,pdf格式生成。但我的客户端也希望以word格式生成报告(.doc格式) 。如何以.doc格式生成报告。

3 个答案:

答案 0 :(得分:4)

对于未来的读者,不推荐使用setParameter,您应该使用这样的导出器:

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

答案 1 :(得分:3)

仅提供基于Alex建议的示例代码:

使用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();
}

与我们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();
}

根据评论进行编辑:

Here是Sprig Fraework提供的JaperReport视图列表。

他们似乎没有专门针对doc格式的。你可能会通过扩展AbstractJasperReportsSingleFormatView来编写自己的。您似乎只需要实现createExporter()方法。

protected JRExporter createExporter(){
     return new JRDocxExporter();
}

答案 2 :(得分:3)

基于JRDocxExporter,您可以扩展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;
}

}

并在@Controller中用HttpServletResponseWrapper包装响应以获取jasper报告生成的字节数组:

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

之后,您将拥有表示Word文档的字节数组。