- 浏览: 884840 次
文章分类
最新评论
-
mxdxm:
总结不错,赞
NoSQL学习笔记(三)之BigTable -
itbj00:
不错,有实际案例的文章,看着简单。
以公司实际应用讲解OpenStack到底是什么 -
追梦--:
赞一个!
数据库插入百万数据 -
enet_java:
<artifactId>PM_Member_EAR ...
使用maven2 打ear包
JasperReport报表设计总结
再我们先理解一下jasperReport的实现过程。笔者不再画图,网上到处都是。
1)定制报表格式。
有二种方式,一种就是写jrxml文件,其实就是xml文件,只不过是后缀名不一样罢了。另一种方式更直接,就是生成一个JasperDesign类的实例,在japsperDesign中自己定义模板。jrxml文件也是通过一个JRXmlLoad加载过来,转成JasperDesign类的实例。也就是说写jrxml文件还需要进行解析,加载。现实中我们使用的报表一般格式比较固定,因而可以通过先使用iReport工具生成模板,再加载解析的方式。这种方式简单,而且可见性强。
2)填充数据。
在最新版(2007.4.30发布)的jasperReports1.3.3中,支持了很多的格式,包括了对于Hibernate的支持。填充数据对于我们经常使用的来说,一般是二种方式,一种方式是通过JDBC连接提供数据源,一种就是通过javaBean的集合提供数据源。当然还有web Service的xml文件提供的。我的建议是,如果你的程序中的统计直接使用Jdbc就可以完成,那么就使用jdbc数据源的方法,反之,使用javaBean的集合是不错的选择,因为这样不会在意你的数据的来源,你也可以任意处理,比如说,要通过权限检查的数据才在报表中生成的话,就可以过滤到不符合权限的数据。
3)显示或打印。
显示,即将JasperReport生成的文件直接显示出来,打印所使用的方式其实就是生成文件,然后调用打印来对文件进行打印。
看起来是不是很简单,对,确实很简单。
笔者主要从事基于B/S模式的开发,相信大部分人的使用也是基于web进行使用。故笔者对于基于web的开发进行详细讲述。
1、工程环境配置。
将以下包加入WEB-INF/lib中。
commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar
以上包是jasperReport必须。
2、iReport的安装
这个很简单,直接下一步就可以了。
3、使用iReport生成 .jasper文件
.jasper是经过编译的japserReport模板,即.jrxml编译后的。
对于iReport的使用网络上已经有了详细的中文的文档,具体可以下载附件查看。使用.jasper文件可以避免在系统中再进行编译,增加系统的压力。
4、数据填充
数据填充是相对于比较重要的一步,数据填充的目的是为了将数据填充进去,生成一个JapserPrint对象。笔者在之前已经阐述过,现在主要是基于关系型数据库(比较简单的,没有权限控制的),以及基于JavaBean Collection进行数据填充。进行数据填充的基本就是一定要实现JRDataSource,JRDataSource定义了二个方法,一个是指针移动的方法next(),另一个是取值的getFieldValue()方法,用来取值填充。
对于关系型数据库,即直接利用数据库的表来生成报表的,只需要传入一个Java Connection即可。JasperReport已经有了个默认的实现的JRDataSource的接口了。对于Java Conncetion的获得,笔者建议使用工厂方法或者使用Spring方式来获得。可以参照笔者写的如下:
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-11
- */
- packagecn.com.reachway.framework.report;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importcn.com.reachway.framework.exception.JasperReportException;
- /**
- *UsedforgetaJDBCconnectionforJasperReport
- */
- publicclassJDBCConnection{
- /**
- *WhichJdbcdriverwereused.Like:"net.sourceforge.jtds.jdbc.Driver"
- */
- privateStringjdbcDriver;
- /**
- *Thejdbcurl,definewhichdatabaseserver,whichdb.Like:
- *"jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"
- */
- privateStringjdbcUrl;
- privateStringdbUser;
- privateStringdbPassword;
- publicStringgetDbPassword(){
- returndbPassword;
- }
- publicvoidsetDbPassword(StringdbPassword){
- this.dbPassword=dbPassword;
- }
- publicStringgetDbUser(){
- returndbUser;
- }
- publicvoidsetDbUser(StringdbUser){
- this.dbUser=dbUser;
- }
- publicStringgetJdbcDriver(){
- returnjdbcDriver;
- }
- publicvoidsetJdbcDriver(StringjdbcDriver){
- this.jdbcDriver=jdbcDriver;
- }
- publicStringgetJdbcUrl(){
- returnjdbcUrl;
- }
- publicvoidsetJdbcUrl(StringjdbcUrl){
- this.jdbcUrl=jdbcUrl;
- }
- publicJDBCConnection(){
- super();
- }
- /**
- *GettheConnection
- *@returnconnection
- *@throwsJasperReportException
- */
- publicConnectiongetConnection()throwsJasperReportException{
- Connectioncon;
- try{
- check();
- Class.forName(this.jdbcDriver);
- con=DriverManager.getConnection(this.jdbcUrl,this.dbUser,this.dbPassword);
- returncon;
- }catch(Exceptione){
- e.printStackTrace();
- thrownewJasperReportException("GetJDBCConnectionError");
- }
- }
- /**
- *ChecktheConfigure
- *@throwsJasperReportException
- */
- privatevoidcheck()throwsJasperReportException{
- if(this.jdbcDriver==null||this.jdbcDriver.equals("")||this.jdbcUrl==null||this.jdbcUrl.equals("")
- ||this.dbUser==null||this.dbUser.equals("")||this.dbPassword==null){
- thrownewJasperReportException("JdbcConfigureerror!");
- }
- }
- }
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-14
- */
- packagecn.com.reachway.framework.report.dataSource;
- importnet.sf.jasperreports.engine.JRDataSource;
- importnet.sf.jasperreports.engine.JRException;
- importnet.sf.jasperreports.engine.JRField;
- /**
- *报表的dataSource的基类,所有利用DataSource产生报表的继承此类
- */
- publicabstractclassReportDataSourceimplementsJRDataSource{
- /**
- *取值
- */
- publicabstractObjectgetFieldValue(JRFieldjrField)throwsJRException;
- /**
- *指针移动
- */
- publicabstractbooleannext()throwsJRException;
- }
对于其它的数据源,JavaBean Collection,笔者采用了一个抽象类,使用抽象类的目的,是为了使开发者尽可能不用直接接触jasperReport(此为笔者自己的方式,笔者是PM,为了考虑底下的人的开发)。以下为抽象类以及笔者提供的一个sample:
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-14
- */
- packagecn.com.reachway.framework.report.dataSource;
- importjava.util.ArrayList;
- importjava.util.List;
- importnet.sf.jasperreports.engine.JRException;
- importnet.sf.jasperreports.engine.JRField;
- /**
- *利用JavaBeanCollection生成ReportDataSource的例子
- */
- publicclassReportDataSourceSampleextendsReportDataSource{
- privateListdocs=newArrayList();
- privateintindex=-1;
- publicReportDataSourceSample(){
- for(inti=0;i<10;i++){
- Documentdoc=newDocument("ViewTimesis:"+i,i);
- this.docs.add(doc);
- }
- }
- //内部类,作为demo时使用
- privateclassDocument{
- privateStringname;
- privateintviewTimes;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicintgetViewTimes(){
- returnviewTimes;
- }
- publicvoidsetViewTimes(intviewTimes){
- this.viewTimes=viewTimes;
- }
- publicDocument(){
- }
- publicDocument(Stringname,intviewTimes){
- this.name=name;
- this.viewTimes=viewTimes;
- }
- }
- publicListgetDocs(){
- returndocs;
- }
- publicvoidsetDocs(Listdocs){
- this.docs=docs;
- }
- @Override
- publicObjectgetFieldValue(JRFieldjrField)throwsJRException{
- StringfieldName=jrField.getName();
- Documentdoc=this.docs.get(index);
- if("name".equals(fieldName)){
- returndoc.getName();
- }
- if("viewTimes".equals(fieldName)){
- returndoc.getViewTimes();
- }
- returnnull;
- }
- @Override
- publicbooleannext()throwsJRException{
- index++;
- return(index<this.docs.size());
- }
- }
以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-12
- */
- packagecn.com.reachway.framework.report.jasperPrint;
- importjava.io.File;
- importjava.sql.Connection;
- importjava.util.Map;
- importnet.sf.jasperreports.engine.JRException;
- importnet.sf.jasperreports.engine.JasperFillManager;
- importnet.sf.jasperreports.engine.JasperPrint;
- importnet.sf.jasperreports.engine.JasperReport;
- importnet.sf.jasperreports.engine.util.JRLoader;
- importcn.com.reachway.framework.exception.JasperReportException;
- /**
- *使用报表模板及数据等来生成JapserPrint
- */
- publicclassJasperPrintWithConnection{
- /**传入的参数*/
- privateMapparams;
- /**模板文件的地址*/
- privateStringreportFilePath;
- /**JDBCconnection*/
- privateConnectioncon;
- publicConnectiongetCon(){
- returncon;
- }
- publicvoidsetCon(Connectioncon){
- this.con=con;
- }
- publicMapgetParams(){
- returnparams;
- }
- publicvoidsetParams(Mapparams){
- this.params=params;
- }
- publicStringgetReportFilePath(){
- returnreportFilePath;
- }
- publicvoidsetReportFilePath(StringreportFilePath)throwsJasperReportException{
- if(reportFilePath==null||!reportFilePath.endsWith(".jasper"))
- thrownewJasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath=reportFilePath;
- }
- publicJasperPrintWithConnection(){
- super();
- }
- publicJasperPrintWithConnection(StringreportFilePath,Mapparams,Connectioncon)throwsJasperReportException{
- if(reportFilePath==null||!reportFilePath.endsWith(".jasper"))
- thrownewJasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if(con==null)
- thrownewJasperReportException("Conncetion不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setCon(con);
- }
- /**
- *取得JasperPrint
- *@return
- *@throwsJasperReportException
- */
- publicJasperPrintgetJasperPrint()throwsJasperReportException{
- FilereportFile=newFile(this.reportFilePath);
- if(!reportFile.exists())
- thrownewJasperReportException("传入的模板文件不存在!");
- try{
- //Load编译好的模板
- JasperReportjasperReport=(JasperReport)JRLoader.loadObject(reportFile.getPath());
- //进行数据填充
- JasperPrintjasperPrint=JasperFillManager.fillReport(jasperReport,this.params,this.con);
- returnjasperPrint;
- }catch(JRExceptionjre){
- jre.printStackTrace();
- thrownewJasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
- }
- }
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-14
- */
- packagecn.com.reachway.framework.report.jasperPrint;
- importjava.io.File;
- importjava.util.Map;
- importnet.sf.jasperreports.engine.JRDataSource;
- importnet.sf.jasperreports.engine.JRException;
- importnet.sf.jasperreports.engine.JasperFillManager;
- importnet.sf.jasperreports.engine.JasperPrint;
- importnet.sf.jasperreports.engine.JasperReport;
- importnet.sf.jasperreports.engine.util.JRLoader;
- importcn.com.reachway.framework.exception.JasperReportException;
- /**
- *
- */
- publicclassJasperPrintWithDataSource{
- /**传入的参数*/
- privateMapparams;
- /**模板文件的地址*/
- privateStringreportFilePath;
- /**dataSrouce*/
- privateJRDataSourcedataSource;
- publicJRDataSourcegetDataSource(){
- returndataSource;
- }
- publicvoidsetDataSource(JRDataSourcedataSource){
- this.dataSource=dataSource;
- }
- publicMapgetParams(){
- returnparams;
- }
- publicvoidsetParams(Mapparams){
- this.params=params;
- }
- publicStringgetReportFilePath(){
- returnreportFilePath;
- }
- publicvoidsetReportFilePath(StringreportFilePath)throwsJasperReportException{
- if(reportFilePath==null||!reportFilePath.endsWith(".jasper"))
- thrownewJasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath=reportFilePath;
- }
- publicJasperPrintWithDataSource(){
- super();
- }
- publicJasperPrintWithDataSource(StringreportFilePath,Mapparams,JRDataSourcedataSource)
- throwsJasperReportException{
- if(reportFilePath==null||!reportFilePath.endsWith(".jasper"))
- thrownewJasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if(dataSource==null)
- thrownewJasperReportException("DataSource不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setDataSource(dataSource);
- }
- /**
- *取得JasperPrint
- *@return
- *@throwsJasperReportException
- */
- publicJasperPrintgetJasperPrint()throwsJasperReportException{
- FilereportFile=newFile(this.reportFilePath);
- if(!reportFile.exists())
- thrownewJasperReportException("传入的模板文件不存在!");
- try{
- //Load编译好的模板
- JasperReportjasperReport=(JasperReport)JRLoader.loadObject(reportFile.getPath());
- //进行数据填充
- JasperPrintjasperPrint=JasperFillManager.fillReport(jasperReport,this.params,this.dataSource);
- returnjasperPrint;
- }catch(JRExceptionjre){
- jre.printStackTrace();
- thrownewJasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
- }
- }
其中使用的JasperReportException为笔者定义的异常,用来统一处理报表异常。
5、报表产生
报表产生是程序中最终可见的一部分,在jasperReport的demo中,大部分中使用了jasperReport的net.sf.jasperreports.j2ee.servlets.*中的类来生成。其实这也算是开源的产品的一个问题,其实jasperReport提供的report的工具,只能算是demo级别的,不能算是产品级别的。相信很多的朋友在使用的时候就碰上无法生成的问题。笔者在此基础上封装了一下。
具体参见以下:
- /**
- *@copyRightBeijingTsing-TechReachwaySoftwareCo.,Ltd.
- *@authorJimmy.Shine2007-5-12
- */
- packagecn.com.reachway.framework.report.export;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.sql.Connection;
- importjava.util.Map;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importnet.sf.jasperreports.engine.JRDataSource;
- importnet.sf.jasperreports.engine.JRExporterParameter;
- importnet.sf.jasperreports.engine.JasperPrint;
- importnet.sf.jasperreports.engine.export.JRHtmlExporter;
- importnet.sf.jasperreports.engine.export.JRHtmlExporterParameter;
- importnet.sf.jasperreports.j2ee.servlets.ImageServlet;
- importcn.com.reachway.framework.exception.JasperReportException;
- importcn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
- importcn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
- /**
- *利用报表生成HTML格式报表
- */
- publicclassHTMLExport{
- /**
- *导出报表
- *
- *@paramrequest
- *@paramresponse
- *@paramreportFilePath
- *@paramparams
- *@paramcon
- *@throwsJasperReportException
- */
- publicvoidexport(HttpServletRequestrequest,HttpServletResponseresponse,StringreportFilePath,Mapparams,
- Connectioncon)throwsJasperReportException{
- try{
- PrintWriterout=response.getWriter();
- try{
- response.setContentType("text/html;charset=UTF-8");
- JasperPrintjasperPrint=newJasperPrintWithConnection(reportFilePath,params,con).getJasperPrint();
- //使用JRHtmlExproter导出Html格式
- JRHtmlExporterexporter=newJRHtmlExporter();
- request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
- exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
- exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,out);
- exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"./servlets/image?image=");
- exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");
- //导出
- exporter.exportReport();
- }catch(Exceptione){
- e.printStackTrace();
- thrownewJasperReportException("在导出Html格式报表时发生错误!");
- }finally{
- if(out!=null){
- try{
- out.close();
- }catch(Exceptione){
- }
- }
- }
- }catch(IOExceptionioe){
- ioe.printStackTrace();
- thrownewJasperReportException("")
- }
相关推荐
jasperreport报表开发小结,使用ireport设计报表模板,javabean作为数据源,整合springboot实现在线预览和导出各种格式的报表
使用ireport和struts2进行报表设计的简单示例,生成普通报表和饼图、柱状图。java工程的lib里包含丰富的jar包,且很多都是从各个网站下载的最新版本,文件夹内还包含一些ireport和jasperreport文档,适合刚入门学习...
jasperReport 进行报表开发 可以输出HTML ,JSP,PDF等格式
iReport-JasperReport 报表开发指南,PDF格式,里面带有图文,入门性质的教程
使用Spring2.5+Struts2+ibatis+jasperreport3.1.4 实现Struts2与Jasperreport报表结合
JasperReport报表成型框架,可直接导入Myeclipse使用,附带文档有相关解释。
JasperReport6.4.0报表设计,webSpringmvc,maven,idea,请使用jdk1.7
ireport-5.6.0 Jasperreport 报表工具 jar包大全Two
JasperReport 中交叉报表指南
ireport-5.6.0 Jasperreport 报表工具 jar包大全
同时,借助开源的JasperReports模板设计利器iReports,可以可视化的设计报表模板。基于Web的报表在很多项目中都是需要的。而随着Struts2的流行,就应该研究一下在Struts2下如何利用JasperReports进行报表输出。本...
iReport-5.6.0 Jasperreport
JasperReport 水晶报表 , 支持子报表 多数据源。
springboot整合JasperReport实现报表功能文章所需的全部资源
在文档中基本将工作中使用IReport和JasperReport开发报表时碰到的问题以及解决方法都予以给出. (看了就知道值)
jasperreport 6.4.1报表动态列,以及生成导出html,可直接导入到eclipse中进行运行,包含一个完整的demo