ireport利用javabean做数据源_第1页
ireport利用javabean做数据源_第2页
ireport利用javabean做数据源_第3页
ireport利用javabean做数据源_第4页
ireport利用javabean做数据源_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/ireport利用javabean做数据源

创建javabean

1.创建DailySales.java,一个简单VObean。

import

java.io.Serializable;public

class

DailySales

implements

Serializable{

private

static

final

long

serialVersionUID

=1L;

private

String

productNo

;

private

String

productName

;

private

int

number

;

private

int

money

;

private

int

id

;

public

DailySales(StringproductNo,StringproductName,

int

number,

int

money)文档来自于网络搜索

{

this

.

productNo

=productNo;

this

.

productName

=productName;

this

.

number

=number;

this

.

money

=money;

}

public

StringgetProductNo()

{

return

productNo

;

}

public

void

setProductNo(StringproductNo)

{

this

.

productNo

=productNo;

}

public

StringgetProductName()

{

return

productName

;

}

public

void

setProductName(StringproductName)

{

this

.

productName

=productName;

}

public

int

getNumber()

{

return

number

;

}

public

void

setNumber(

int

number)

{

this

.

number

=number;

}

public

int

getMoney()

{

return

money

;

}

public

void

setMoney(

int

money)

{

this

.

money

=money;

}

public

int

getId()

{

return

id

;

}

public

void

setId(

int

id)

{

this

.

id

=id;

}

}2.

创建

DailySalesDataSource.java,

这是报表的数据源。这个类实现了

jasperreports

中提供的数据源接口

JRDataSource,

实现其中的两个方法

:next()

getFieldValue(JRFieldfield)

。文档来自于网络搜索import

net.sf.jasperreports.engine.JRDataSource;import

net.sf.jasperreports.engine.JRException;import

net.sf.jasperreports.engine.JRField;

public

class

DailySalesDataSource

implements

JRDataSource文档来自于网络搜索{

/**

*

测试数据,实际项目中是动态获取,也不一定是数组,可以是其它的数据类型

.

*/

private

Object[][]

data

=

{

{

"

货号

1"

,

"

物品1

"

,1,1000},

{

"

货号

2"

,

"

物品

2"

,2,2000},

{

"

货号

3"

,

"

物品

3"

,3,3000},

{

"

货号

4"

,

"

物品

4"

,4,4000},

{

"

货号

5"

,

"

物品

5"

,5,5000},

{

"

货号

6"

,

"

物品

6"

,6,6000},

{

"

货号

7"

,

"

物品

7"

,7,7000},

{

"

货号

8"

,

"

物品

8"

,8,8000},

{

"

货号

9"

,

"

物品

9"

,9,9000},

{

"

货号

10"

,

"

物品

10"

,10,10000}

};

private

int

index

=-1;

public

DailySalesDataSource()

{

}/**

*

实现了

JRDataSource

中的方法.判断是否还有下一个.

*/

public

boolean

next()

throws

JRException

{

index

++;

return

(

index

<

data

.

length

);

}

/**

*

实现了

JRDataSource

中的方法.

*

@param

field

是对应报表中的要填充的字段的名称.

*/

public

ObjectgetFieldValue(JRField

field

)

throws

JRException文档来自于网络搜索

{

Objectvalue=

null

;

StringfieldName=

field

.getName();

if

(

"id"

.equals(fieldName))

{

value=

index+1

;

}

else

if

(

"productNo"

.equals(fieldName))

{

value=

data

[

index

][0];

}

else

if

(

"productName"

.equals(fieldName))

{

value=

data

[

index

][1];

}

else

if

(

"number"

.equals(fieldName))

{

value=

data

[

index

][2];

}

else

if

(

"money"

.equals(fieldName))

{

value=

data

[

index

][3];

}

return

value;

}}3

.在

ireport

中使用,取得测试数据源

,

如果不使用

ireport

工具,则只需要上面的两个类。文档来自于网络搜索import

java.util.Arrays;import

java.util.Collection;/***

简单工厂类,取得测试数据*

@author

xmlin**/public

class

DailySalesFactory{

private

static

DailySales[]

data

=

{

new

DailySales(

"

货号

1"

,

"

物品1

"

,1,1000),

new

DailySales(

"

货号

2"

,

"

物品

2"

,2,2000),

new

DailySales(

"

货号

3"

,

"

物品

3"

,3,3000),

new

DailySales(

"

货号

4"

,

"

物品

4"

,4,4000),

new

DailySales(

"

货号

5"

,

"

物品

5"

,5,5000),

new

DailySales(

"

货号

6"

,

"

物品

6"

,6,6000),

new

DailySales(

"

货号

7"

,

"

物品

7"

,7,7000),

new

DailySales(

"

货号

8"

,

"

物品

8"

,8,8000),

new

DailySales(

"

货号

9"

,

"

物品

9"

,9,9000),

new

DailySales(

"

货号

10"

,

"

物品

10"

,10,10000)

};

public

static

Object[]getBeanArray()

{

return

data

;

}

public

static

CollectiongetBeanCollection()

{

return

Arrays.asList

(

data

);

}}三.使用ireport开发报表样式1.新建一个项目。2.设置类路径,在菜单“options”中选择Classpath,点击在弹出框中的addfolder,填写javabean编译成的.class文件存放的路径.点saveClasspath完成。如图文档来自于网络搜索

3.设置数据源.在菜单"Data"中选择”Connection/Datasources”,点击在弹出框中的new按钮增加一个数据源.如图

其中Name随便取一个名字,TypeofConnection/Data选择JavaBeanssetdatasource,如果使用其它的数据源则选择其它的选项.Factoryclass为我们刚才创建的Factory类,里面包含取得测试数据的静态方法getBeanCollection().用Test测试是否成功,点Save保存.文档来自于网络搜索4.设置活动连接.在菜单"Data"中选择”SetActiveConnection”.5.ReportQuery,在菜单"Data"中选择”ReportQuery”,填写javabean,即我们创建的VObean.如图文档来自于网络搜索6.设计报表.

设计日期字段如图

设计填充字段,如图设计页数字段如图设计好的报表样式如图点菜单"build"中的"compile"进行编译,然后再”executewithconnectiondatasource”就可以看到报表的结果了.

报表设计完成,在ireport的执行路径下会生成一个DailySales.jasper的文件.

四.编写测试类.

生成的DailySales.jasper可以在web服务端生成报表,也可以在肥客户端如swing生成,这里写一个在肥客户端和简单运用.文档来自于网络搜索import

java.awt.Dimension;import

java.util.HashMap;import

java.util.Map;import

net.sf.jasperreports.engine.JRException;import

net.sf.jasperreports.engine.JasperFillManager;文档来自于网络搜索import

net.sf.jasperreports.engine.JasperPrint;import

net.sf.jasperreports.engine.JasperReport;import

net.sf.jasperreports.engine.util.JRLoader;import

net.sf.jasperreports.view.JasperViewer;public

class

TestReport{

public

static

void

main(String[]args)

{

TestReport.showReport

();

}

private

static

void

showReport()

{

StringreportPath=

"D:\\dailySales.jasper"

;

Mapparameters=

new

HashMap();

//

如果报表中有用到变量,在这里给它赋值.

//parameters.p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论