4

我正在使用 jasper-reports 4.5.0、spring 3.0.5RELEASE 集成来生成报告。直到现在我以 html、csv、pdf 格式生成。但我的客户也想要 word 格式的报告(.doc 格式)。如何我以这种 .doc 格式生成报告。

4

3 回答 3

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();
于 2015-05-27T15:21:15.863 回答
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();
}

根据评论编辑:

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

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

protected JRExporter createExporter(){
     return new JRDocxExporter();
}
于 2012-06-07T14:13:28.243 回答
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;
}

}

并在您使用HttpServletResponseWrapper@Controller包装响应以获取由 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 文档的字节数组。

于 2012-11-16T12:47:39.670 回答