jasperreports自定义数据源
来源:互联网 发布:英语短语软件 编辑:程序博客网 时间:2024/06/07 05:49
使用的是jasperreports-5.6.0版本的包。
需求: 报表由一个基本Basic对象和一个集合类CustomList对象组成
问题:jasperreports没有提供类似可用的datasource类
解决方法:自定义一个DataSource实现JRDataSource接口即可
仿照JRBeanCollectionDataSource写了一个ReportDataSource,同样使其继承JRAbstractBeanDataSource,代码如下:
public class ReportDataSource extends JRAbstractBeanDataSource { /** * */ private Collection<?> data; private Iterator<?> iterator; private Object currentBean; private Object basicData;//基本数据 /** * */ public ReportDataSource(Object basicData,Collection<?> beanCollection) { this(basicData,beanCollection, true); } /** * */ public ReportDataSource(Object basicData,Collection<?> beanCollection, boolean isUseFieldDescription) { super(isUseFieldDescription); this.basicData = basicData; this.data = beanCollection; if (this.data != null) { this.iterator = this.data.iterator(); } } /** * */ public boolean next() { boolean hasNext = false; if (this.iterator != null) { hasNext = this.iterator.hasNext(); if (hasNext) { this.currentBean = this.iterator.next(); } } return hasNext; } /** * */ public Object getFieldValue(JRField field) throws JRException { if(field.getName().contains("basic.")){ return getBeanProperty(basicData,field.getName().split("\\.")[1]); } return getFieldValue(currentBean, field); } /** * */ public void moveFirst() { if (this.data != null) { this.iterator = this.data.iterator(); } } /** * Returns the underlying bean collection used by this data source. * * @return the underlying bean collection */ public Collection<?> getData() { return data; } /** * Returns the total number of records/beans that this data source * contains. * * @return the total number of records of this data source */ public int getRecordCount() { return data == null ? 0 : data.size(); } /** * Clones this data source by creating a new instance that reuses the same * underlying bean collection. * * @return a clone of this data source */ public ReportDataSource cloneDataSource() { return new ReportDataSource(basicData,data); }上面代码是修改JRBeanCollectionDataSource的,仅仅变动了几行代码而已,在jrmxl文件中就可以使用basic.属性名来标识Basic对象的属性了。
值得注意的是,如果CustomList为空,则无法输出报表,如果需要,可以在ReportDataSource构造器中增加 判断集合是否为空的代码,如果为空则初始化其长度为1的集合
阅读全文
0 0
- jasperreports自定义数据源
- JasperReports
- JasperReports
- JasperReports
- JasperReports
- JasperReports
- jasperReports
- 如何在JasperReports中使用XML文件作为数据源?
- 如何在JasperReports中使用CSV文件作为数据源?
- 改造jasperreports for struts2插件,增加多种数据源提供能力
- JasperReports报表数据源—JDBC、JavaBeans、TableModel、XML、CSV
- 创建自定义数据源
- 自定义GridView/ListView数据源
- 润乾 自定义数据源
- Sharpmap自定义数据源
- MyDataSource自定义的数据源
- yii 自定义数据源
- 自定义GridView/ListView数据源
- Hdu 6007 Mr. Panda and Crystal 最短路+完全背包
- LeetCode 399 Evaluate Division(BFS)
- 关联分析之Apriori算法
- 数据可视化概念
- JS JAVA MD5 计算
- jasperreports自定义数据源
- Summary:learn python 2
- shiro讲解 之 Authentication-Authorization小结
- CentOS7配置IP和网络问题排查
- 《java编程思想》第三章笔记之自增自减运算符
- JasperReport 6 使用image
- C 判断循环
- springmvc+shiro用户登录后获取用户
- Eclipse Tomcat访问webapps下其他目录失败