步骤
(1)下载最新的iReport,且安装(http://community.jaspersoft.com/download)
(2)用iReport设计报表模板(http://community.jaspersoft.com/wiki/designing-report 有个简单的例子,可以作为参考)
(3)将以下包加入WEB-INF/lib中。
commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar
以上包是jasperReport必须。
(4)编写xhtml文件,样例代码如下:
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandbutton actionlistener="#{demoBean.DOCX}" value="Docx" ajax="false" />
</h:form>
</h:body>
上面的例子中一定要注意的是必须有ajax="false"属性
(5)编写manager bean
public void DOCX(ActionEvent actionEvent) throws JRException, IOException{ HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.docx"); ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream(); JRDocxExporter docxExporter=new JRDocxExporter(); docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream); docxExporter.exportReport(); FacesContext.getCurrentInstance().responseComplete(); }
getOutputStream() has already been called for this response
关于ireport的设计细节可以参考iReport的文档