环境要求:到http://www.eclipse.org/downloads/下载?
Eclipse IDE for Java and Report Developers 工具
第一步:创建一java项目ReportJava;
第二步:创建包yss.com并编写一实体类User 作为报表中的数据对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package ?com.yss; public ?class ?User?{ ???? private ?String?name; ???? private ?String?phone; ???? private ?String?address; ???? ????? public ?User(){} ???? ????? public ?User(String?name,String?phone,String?address){ ???????? this .name=name; ???????? this .phone=phone; ???????? this .address=address; ???? } ???? ????? public ?void ?setName(String?name)?{ ???????? this .name?=?name; ???? } ???? public ?String?getName()?{ ???????? return ?name; ???? } ???? public ?String?getPhone()?{ ???????? return ?phone; ???? } ???? public ?void ?setPhone(String?phone)?{ ???????? this .phone?=?phone; ???? } ???? public ?String?getAddress()?{ ???????? return ?address; ???? } ???? public ?void ?setAddress(String?address)?{ ???????? this .address?=?address; ???? } } |
创建一User POJO的工厂类UserFactory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package ?com.yss; import ?java.util.ArrayList; import ?java.util.Collection; import ?java.util.List; public ?class ?UserFactory?{ ???? public ?List<User>?createUser(){ ???????? List<User>?users= new ?ArrayList<User>(); ???????? User?u1= new ?User( "qing" , "12446" , "深圳" ); ???????? User?u2= new ?User( "long" , "34642" , "长沙" ); ???????? User?u3= new ?User( "hai" , "74532" , "上海" ); ???????? ????????? users.add(u1); ???????? users.add(u2); ???????? users.add(u3); ???????? return ?users; ???? } } |
第三步:新建一report 名为report.rptdesign ,
??????? 下一步:设置Report templates:Blank Report 完成
??????? 并且切换到Report Design试图下;
第四步:在左边切换到层面板Data Explorer 操作,
??????? 右击Data Sources新建一个data sources,在此构建中选择Scripted Data Source并命名为pojoSources; 完成
第五步:右击Data Sets新建一Set数据集,选择刚建的数据源pojoSources,
??????? 在output columns中输入你要显示对应的pojo类相关信息, 完成
??????? 选中新建的set集 在编辑区中的Script:open
?????? 加入脚本:
1 2 3 | count= 0 ; uf= new ?Packages.com.yss.UserFactory(); users=uf.createUser(); |
再将Script切换到fetch 并加入脚本:
1 2 3 4 5 6 7 8 | if (count<users.size()){ ???? row[ "name" ]=users.get(count).getName(); ???? row[ "phone" ]=users.get(count).getPhone(); ???? row[ "address" ]=users.get(count).getAddress(); ???? count++; ???? return ?true ; } return ?false ; |
在将Script切换到close 清除对象:
?????? uf=null;
?????? users=null;
第六步:将报表的编辑状态 切换到 layout 下,左边的层面板切换到Palette:拖一table组件到编辑区,在将之前建的set集拖至table上;
ok 啦。。可以再编辑区中切换到preview预览的你结果啦!