JasperReport使用自定义的JRDataSource创建报表
前面介绍了使用Collection of JavaBeans方式创建报表,其实我们还可以通过自定义实现JRDataSource接口的方式,为了方便此处直接继承JRBeanCollectionDataSource,同样的此处也需要增加一个静态工厂方法
package org.example.datasource; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import org.example.entity.User; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class CustomerBeanCollectionDataSource extends JRBeanCollectionDataSource { public CustomerBeanCollectionDataSource(Collection<?> beanCollection) { super(beanCollection); } public CustomerBeanCollectionDataSource(Collection<?> beanCollection, boolean isUseFieldDescription) { super(beanCollection, isUseFieldDescription); } public static CustomerBeanCollectionDataSource getInstance(){ List<User> list = new ArrayList<>(); for(int i=0;i<10;i++){ User user = new User(); user.setName("AA"+i); user.setAge(20+i); user.setSex("男"); user.setAddress("address"+i); list.add(user); } return new CustomerBeanCollectionDataSource(list); } }
此处用到了前面定义的User,其他步骤和上面类似,就不过多讲述了,具体参见 https://www.cnblogs.com/xiupan/p/13334986.html