`
javaliujie
  • 浏览: 58312 次
  • 性别: 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? 请指教``

相关推荐

Global site tag (gtag.js) - Google Analytics