`
javaliujie
  • 浏览: 59919 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

jasperreport学习 之 javabean封装成list作为数据源

阅读更多

jasperreport学习之javabean封装成list作为数据源

              原创 by javaliujie  http://javaliujie.iteye.com/blog/278936

 

1.思想

 javabean
    ↓
    ↓封装
    ↓
Collection
    ↓
    ↓作为数据源
    ↓
JRDataSource

 

2.代码部分

CustomBean.java

 

 

package cn.com.jr;

public class CustomBean
{
	private String city = null;
	private Integer id = null;
	private String name = null;
	private String street = null;

	public CustomBean(String pcity,Integer pid,String pname,String pstreet)
	{
		city = pcity;
		id = pid;
		name = pname;
		street = pstreet;
	}
	public CustomBean getMe()
	{
		return this;
	}
	public String getCity()
	{
		return city;
	}
	public Integer getId()
	{
		return id;
	}
	public String getName()
	{
		return name;
	}
	public String getStreet()
	{
		return street;
	}
}

  

 

 

 

 

CustomBeanFactory.java

 

package cn.com.jr;

import java.util.Arrays;
import java.util.Collection;

public class CustomBeanFactory
{
	private static CustomBean[] data =
		{
			new CustomBean("Berne", new Integer(9), "James Schneider", "277 Seventh Av."),
			new CustomBean("Berne", new Integer(22), "Bill Ott", "250 - 20th Ave."),
			new CustomBean("Boston", new Integer(23), "Julia Heiniger", "358 College Av."),
			new CustomBean("Boston", new Integer(32), "Michael Ott", "339 College Av."),
			new CustomBean("Chicago", new Integer(39), "Mary Karsen", "202 College Av."),
			new CustomBean("Chicago", new Integer(35), "George Karsen", "412 College Av."),
			new CustomBean("Chicago", new Integer(11), "Julia White", "412 Upland Pl."),
			new CustomBean("Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."),
			new CustomBean("Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."),
			new CustomBean("Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."),
			new CustomBean("Dallas", new Integer(36), "John Steel", "276 Upland Pl."),
			new CustomBean("Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."),
			new CustomBean("Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."),
			new CustomBean("Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."),
			new CustomBean("Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."),
			new CustomBean("Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."),
			new CustomBean("Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."),
			new CustomBean("Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."),
			new CustomBean("Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."),
			new CustomBean("Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."),
			new CustomBean("New York", new Integer(46), "Andrew May", "172 Seventh Av."),
			new CustomBean("New York", new Integer(44), "Sylvia Ott", "361 College Av."),
			new CustomBean("New York", new Integer(41), "Bill King", "546 College Av."),
			new CustomBean("Oslo", new Integer(45), "Janet May", "396 Seventh Av."),
			new CustomBean("Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."),
			new CustomBean("Paris", new Integer(25), "Sylvia Steel", "269 College Av."),
			new CustomBean("Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."),
			new CustomBean("Paris", new Integer(5), "Laura Miller", "294 Seventh Av."),
			new CustomBean("San Francisco", new Integer(48), "Robert White", "549 Seventh Av."),
			new CustomBean("San Francisco", new Integer(7), "James Peterson", "231 Upland Pl.")
		};  
	public static Object[] getBeanArray()
	{
		return data;
	}
	public static Collection getBeanCollection()
	{
		return Arrays.asList(data);
	}


}

 

 

 

 

 

 

 

 

3.iReport设置

  a.设置环境变量

 

 

 

 

 

 

 

 

  b.查询获得变量

 

 

c.iReport页面

 

 

 

  d.iReport最终效果图

 

 

 

 

4.与程序结合代码

 

CustomDataSource.java

package cn.com.jr;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class CustomDataSource implements JRDataSource
{
	private Object[][] data =
		{
			{"Berne", new Integer(22), "Bill Ott", "250 - 20th Ave."},
			{"Berne", new Integer(9), "James Schneider", "277 Seventh Av."},
			{"Boston", new Integer(32), "Michael Ott", "339 College Av."},
			{"Boston", new Integer(23), "Julia Heiniger", "358 College Av."},
			{"Chicago", new Integer(39), "Mary Karsen", "202 College Av."},
			{"Chicago", new Integer(35), "George Karsen", "412 College Av."},
			{"Chicago", new Integer(11), "Julia White", "412 Upland Pl."},
			{"Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."},
			{"Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."},
			{"Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."},
			{"Dallas", new Integer(36), "John Steel", "276 Upland Pl."},
			{"Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."},
			{"Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."},
			{"Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."},
			{"Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."},
			{"Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."},
			{"Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."},
			{"Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."},
			{"Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."},
			{"Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."},
			{"New York", new Integer(46), "Andrew May", "172 Seventh Av."},
			{"New York", new Integer(44), "Sylvia Ott", "361 College Av."},
			{"New York", new Integer(41), "Bill King", "546 College Av."},
			{"Oslo", new Integer(45), "Janet May", "396 Seventh Av."},
			{"Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."},
			{"Paris", new Integer(25), "Sylvia Steel", "269 College Av."},
			{"Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."},
			{"Paris", new Integer(5), "Laura Miller", "294 Seventh Av."},
			{"San Francisco", new Integer(48), "Robert White", "549 Seventh Av."},
			{"San Francisco", new Integer(7), "James Peterson", "231 Upland Pl."}
		};

	private int index = -1;
	public CustomDataSource()
	{
	}
	public boolean next() throws JRException
	{
		index++;
		return (index < data.length);
	}

	public Object getFieldValue(JRField field) throws JRException
	{
		Object value = null;
		
		String fieldName = field.getName();
		
		if ("the_city".equals(fieldName))
		{
			value = data[index][0];
		}
		else if ("id".equals(fieldName))
		{
			value = data[index][1];
		}
		else if ("name".equals(fieldName))
		{
			value = data[index][2];
		}
		else if ("street".equals(fieldName))
		{
			value = data[index][3];
		}
		return value;
	}
}

 

 

 

TestPDF.java

public class TestPdf { 

    public static void main(String args[]) {
          File reportFile = new File("javabeantest.jasper");
          CustomDataSource cds = new CustomDataSource() ;
          try {
			JasperRunManager.runReportToPdfFile(reportFile.getPath(), "c:\\javabeantest.pdf", new HashMap(), cds) ;
		} catch (JRException e) {
			e.printStackTrace();
		}
    } 
}

原创 by javaliujie  http://javaliujie.iteye.com/blog/278936

 

分享到:
评论
9 楼 reny0oo 2016-05-31  
你好,生成pdf依赖的jar能不能给我发一下,邮箱:137735058@qq.com
8 楼 cry615 2012-06-28  
文章不错,简单易懂
7 楼 ArronHoo 2011-08-23  
SpringJava 写道
我的在3.iReport设置 a.设置环境变量处的classpath,怎么在测试链接的时候连接提示notFoundClass:check you classpath...

希望这位朋友多多指教!谢谢!

6 楼 SpringJava 2009-09-21  
还有那个javabeanset.jar是怎么弄的谢谢。希望得到答复...........
5 楼 SpringJava 2009-09-21  
我的在3.iReport设置 a.设置环境变量处的classpath,怎么在测试链接的时候连接提示notFoundClass:check you classpath...

希望这位朋友多多指教!谢谢!
4 楼 javaliujie 2008-12-19  
ch3212 写道

为什么我显示出来只有一行数据?没有进行循环

页面设置大小问题
3 楼 ch3212 2008-12-17  
为什么我显示出来只有一行数据?没有进行循环
2 楼 javaliujie 2008-12-08  
limeng530_1987 写道
为什么执行后. 数据中只是显示 name和street,id呢?
而city不显示 city为null. why? 请指教``

如果没有改动程序,那就是java bean source中没有把此字段读进来。
1 楼 limeng530_1987 2008-12-08  
为什么执行后. 数据中只是显示 name和street,id呢?
而city不显示 city为null. why? 请指教``

相关推荐

    jasperreport学习 之 javabean封装成list作为数据源.pdf

    总而言之,JasperReport学习之JavaBean封装成List作为数据源的教程,不仅提供了一套完整的实现流程,还强调了在实际开发中实践和应用的重要性。通过本文档的学习,开发者将能够快速掌握如何将复杂的业务数据封装为...

    jasperreports 使用教程资料

    最后,《jasperreport学习之javabean封装成list作为数据源.pdf》这份资料专门探讨了如何将 JavaBean 数据封装到 List 中用于报表。在实际项目中,JavaBean 往往是数据模型的代表,这篇教程可能包含如何创建 JavaBean...

    使用JavaBean构造JasperReport子报表

    这个集合将作为数据源传递给JasperReport。使用`JRBeanCollectionDataSource`类,你可以将JavaBean集合转换为JasperReport能够理解的数据源。 ```java List&lt;StudentBean&gt; students = ... // 初始化学生数据 ...

    ireport中table使用javaBean数据源

    本知识点主要探讨的是如何在iReport中使用JavaBean作为数据源来填充表格(Table)组件。我们将深入理解这个过程,并结合提供的资源进行详细阐述。 1. **JavaBean数据源的原理** JavaBean是一种符合特定规范的Java...

    图示ireport中使用javabean作数据源开发基于jasperreports报表过程

    在这个阶段,你可能需要设置数据源的连接参数,比如数据库连接信息,或者指向包含`JavaBean`对象的集合(如List或ArrayList)的引用。 6. **编译和运行报表**: 完成设计后,编译报表模板(`.jrxml`文件),`iReport...

    ireport报表制作教程

    在本教程中,我们将学习如何使用ireport和javabean作为数据源来创建PDF报表,并通过list组件处理子报表的问题,以展示多实体数据。 首先,理解ireport的基础知识是至关重要的。ireport提供了一个图形化的用户界面,...

Global site tag (gtag.js) - Google Analytics