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

图示ireport中使用javabean作数据源开发基于jasperreports报表过程
——学习笔记系列之ireport起步
xmlin



本文不讲原理,因为网上的资源很多,本文以一个简单的日销售报表为例,记录在ireport中使用javabean作数据源开发基于jasperreports报表过程.

一.准备工作


1.
正确安装jdk
2.
http://ireport.sourceforge.net/去下载ireport,本文使用1.3.1版本,解压iReport在任意目录,解压后的文件里面有一个iReport.bat,通过双击运行。
3.
http://jasperreports.sourceforge.net/index.html下载jasperreport,本文使用的是1.3.1版本,解压到任意目录,开发中需要使用其中的jasperreports-1.3.1.jarcommons-logging-1.0.2.jarcommons-collections-2.1.jar,使用bean做为数据源还需要commons-beanutils-1.7.jar,如果需要生成pdf文件,还需要itext-1.3.1.jariTextAsian.jar,如果使用动态编译jrxml文件,还需要commons-digester-1.7.jar


二.创建javabean


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

importjava.io.Serializable;

publicclassDailySales implementsSerializable

{

privatestaticfinallongserialVersionUID= 1L;

privateString productNo;

privateString productName;

privateintnumber;

privateintmoney;

privateintid;

publicDailySales(String productNo, String productName, intnumber, intmoney)

{

this.productNo= productNo;

this.productName= productName;

this.number= number;

this.money= money;

}

publicString getProductNo()

{

returnproductNo;

}

publicvoidsetProductNo(String productNo)

{

this.productNo= productNo;

}

publicString getProductName()

{

returnproductName;

}

publicvoidsetProductName(String productName)

{

this.productName= productName;

}

publicintgetNumber()

{

returnnumber;

}

publicvoidsetNumber(intnumber)

{

this.number= number;

}

publicintgetMoney()

{

returnmoney;

}

publicvoidsetMoney(intmoney)

{

this.money= money;

}

publicintgetId()

{

returnid;

}

publicvoidsetId(intid)

{

this.id= id;

}

}



2.生成数据源


importjava.util.Arrays;

importjava.util.Collection;

/**

*简单工厂类,取得数据

*@authorxmlin

*

*/

publicclassDailySalesFactory

{

//为了在ireport中能够测试,必须使用static方法

publicstaticCollection<DailySales> getBeanCollection()

{

//数据源的生成,通常调用dao操作

List<DailySales>data = newArrayList<DailySales>();

for(inti = 0; i < 100; i++)

{

data.add(newDailySales("货号"+ i, "产品"+ i, i, i * 100));

}

returndata;

}

}


三.使用ireport开发报表样式

1.新建一个项目。

2.设置类路径,在菜单“options”中选择Classpath,点击在弹出框中的addfolder,填写javabean编译成的.class文件存放的路径.saveClasspath完成。如图


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

其中Name随便取一个名字,Typeof Connection/Data 选择JavaBeansset data source,如果使用其它的数据源则选择其它的选项.Factoryclass为我们刚才创建的Factory类,里面包含取得测试数据的静态方法getBeanCollection().用Test测试是否成功,点Save保存.

4.设置活动连接.在菜单"Data"中选择”SetActive Connection”.

5.ReportQuery , 在菜单"Data"中选择”ReportQuery”,填写javabean,即我们创建的VObean.如图

.设计报表.
设计日期字段如图

设计填充字段,如图

设计页数字段如图


设计好的报表样式如图

点菜单"build"中的"compile"进行编译,然后再”execute with connection datasource”就可以看到报表的结果了.
报表设计完成,在ireport的执行路径下会生成一个DailySales.jasper的文件.

四.生成报表


生成的DailySales.jasper可以在web服务端生成报表,也可以在肥客户端如swing生成,这里写一个在肥客户端和简单运用.


importjava.util.Arrays;

importjava.util.Collection;

/**

*简单工厂类,取得数据

*@authorxmlin

*

*/

publicclassDailySalesFactory

{

//为了在ireport中能够测试,必须使用static方法

publicstaticCollection<DailySales> getBeanCollection()

{

//数据源的生成,通常调用dao操作

List<DailySales>data = newArrayList<DailySales>();

for(inti = 0; i < 100; i++)

{

data.add(newDailySales("货号"+ i, "产品"+ i, i, i * 100));

}

returndata;

}

}


运行生成的结果如图


生成各种格式的报表文件:


publicstaticvoidmain(String[] args)

{

try

{

//编译报表,并jrxml所在的目录中生成jasper文件

JasperCompileManager.compileReportToFile("d:\\test.jrxml");

Mapparam = newHashMap();

JasperPrintjasperPrint = JasperFillManager.fillReport("d:\\test.jasper",param,

newJRBeanCollectionDataSource(DailySalesFactory.getBeanCollection()));

//生成html和pdf也可以使用JRExporter来生成

JasperExportManager.exportReportToHtmlFile(jasperPrint,"d:\\test.html");

JasperExportManager.exportReportToPdfFile(jasperPrint,"d:\\test.pdf");

//使用JRExporter来生成XLS,很多参数可以查api或ireport的属性窗口

JRExporterxlsExporter = newJRXlsExporter();

ByteArrayOutputStreamxlsOut = newByteArrayOutputStream();

xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,xlsOut);

xlsExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);//删除记录最下面的空行

xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);//删除多余的ColumnHeader

xlsExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);//显示边框

xlsExporter.exportReport();

FileOutputStreamout = newFileOutputStream(newFile("d:\\test.xls"));

out.write(xlsOut.toByteArray());

//使用JRExporter来生成html,很多参数可以查api或ireport的属性窗口

JRExporterhtmlExporter = newJRHtmlExporter();

ByteArrayOutputStreamhtmlOut = newByteArrayOutputStream();

htmlExporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT,jasperPrint);

htmlExporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM,htmlOut);

htmlExporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING,"utf-8");

htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);

htmlExporter.exportReport();

FileOutputStreamout2 = newFileOutputStream(newFile("d:\\test1.html"));

out2.write(htmlOut.toByteArray());

}

catch(Exception e)

{

//TODOAuto-generated catch block

e.printStackTrace();

}

}

本文转载自:http://eneasy.iteye.com/blog/123348


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值