IREPORT使用JAVABEAN作为报表数据源_第1页
IREPORT使用JAVABEAN作为报表数据源_第2页
IREPORT使用JAVABEAN作为报表数据源_第3页
IREPORT使用JAVABEAN作为报表数据源_第4页
IREPORT使用JAVABEAN作为报表数据源_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、使用JDBC作为报表数据源其实是有很多缺陷的,例如,它要求使用SQL查询作为数据来源,但有时报表的内容并不能由一条SQL语句完成。因此,使 用JavaBean集合作为数据源才是终极的解决之道,用户只需要将数据整理到一个JavaBean集合之中就可以了,无论用什么方法,数据源为何物。3.1 定义Bean 定义一个最简单的Bean,只包含一个属性,如下所示: package lld.test.ireport;public class ProductBean.    private String pr

2、oductName;    public String getProductName()    .        return productName;        public void setProductName(String productName)   &#

3、160;.        ductName = productName;     3.2 在iReport中设定数据源及创建报表要在iReport中使用JavaBean作为数据源,首先要定义好Classpath,以使iReport能够找到我们定义的Bean,使用菜单 “Options”-“Classpath”定义Classpath,定义的路径为编译后的.class文件所在路径,例如,我定义的Classpath 为“D:WorkJavaire

4、port_testWebContentWEB-INFclasses”。在菜单“Data”-“Connections/Datasources”菜单中,添加数据源,将数据源类型设为“JavaBeans set data source”,设定的数据源属性如下图所示:按道理而言,上图中的“Factory class”和 “The static method to call to retrieve the array or the collection of javaBeans”中,应该填写相应的工厂类及方法,然后,我们就能够在iReport集成环境中测试输出结果,但就我测试结果而言,发现在我赋上实际

5、的工厂类后,按下Test按钮后,总是提示“The method doen''t return a valid array or java.util.Collection”。所以,我们就只把iReport作为一个报表编辑器好了,不要对它报太大奢望,毕竟不花钱,有点bug(或 者是我猪头没找对方法)也就原谅一下了。将其设为当前数据源,然后到菜单“Data”-“Report Query”中定义需要用到的 Bean属性,在“Class name”中输入自定义Bean的名称,本例中为“lld.test.ireport.ProductBean”,然后点击“Read attributes”按

6、钮,获取Bean属性,点击“Add Selected Field(s)”添加要用的属性,如下图所示:秉承本人所作笔记的一贯风格,只作最主要的功能描述,用最简单的代码描述最核心的功能,因此定义的报表非常之简单,各位如果有兴趣的话,可以加上边框线之类的美化元素,如下图所示:该文件名为report_2.jrxml,编译即可获取report_2.jasper3.3 编程导出PDF 对于Web应用,以PDF格式导出报表是最常见的应用,另外如果是使用C/S模式的话,还可以导出来JRViewer格式。至于其它格式,例如Excel、HTML、Java2D图形等,基本上是自找麻烦,因为导出来的格式很难看,用户会

7、有意见,不如自己去手工生成的好。对于JavaBean集合,最终仍然要转化成为JRDataSource以供JasperReport使用,不过这个转换很简单,一行代码即足够,示例代码如下所示:List<?> data = new ArrayList<?>();.(填充数据)/生成JRDataSourceJRDataSource dataSource = new JRBeanCollectionDataSource(data); 下面的代码是一个生成PDF的完整示例(Servlet):packag

8、e lld.test.ireport;import java.io.IOException;import java.io.OutputStream;import .URLEncoder;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServlet

9、Request;import javax.servlet.http.HttpServletResponse;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRExporterParameter;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf

10、.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;import net.sf.jasperreports.engine.export.JRPdfExporter;import net.sf.jasperreports.engine.util.JRLoader;import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;public cla

11、ss BeanReportServlet extends HttpServlet.    private static final long serialVersionUID = 348226870594216833L;    Override    protected void doGet(HttpServletRequest req, Http

12、ServletResponse resp)            throws ServletException, IOException    .        this.doPost(req, resp);        Overrid

13、e    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException    .      

14、  try        .            /生成测试数据            ArrayList<ProductBean> data = new ArrayList<ProductBean&

15、gt;();            for(int i = 1; i <= 100; i+)            .              

16、;  ProductBean bean = new ProductBean();                bean.setProductName("Product " + i);            &

17、#160;   data.add(bean);                        JRDataSource dataSource = new JRBeanCollectionDataSource(data);      

18、;                  /获取报表模板文件            String root_path = this.getServletContext().getRealPath("/");    

19、        root_path = root_path.replace('''', ''/'');            String reportFilePath = root_path + "WEB-INF/classes/lld/tes

20、t/ireport/report_2.jasper"            System.out.println("jasper file is " + reportFilePath);                  

21、;      /生成JasperPrint            JasperReport report = (JasperReport)JRLoader.loadObject(reportFilePath);            JasperPrint j

22、asperPrint = JasperFillManager.fillReport(report, null, dataSource);            /设定输出格式            OutputStream ouputStream = resp.getOutputStrea

23、m();              resp.setContentType("application/pdf");            resp.setCharacterEncoding("UTF-8");       

24、0;      resp.setHeader("Content-Disposition", "attachment; filename=""                      + URLEncoder.encode("PD

25、F报表", "UTF-8") + ".pdf"");                                  / 使用JRPdfExproter导出器

26、导出pdf              JRPdfExporter exporter = new JRPdfExporter();              / 设置JasperPrintList      &#

27、160;       exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);              exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);              exporter.exportReport();           

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论