Java开源报表JasperReport、iReport4.5.1使用详解(一)

简介:

Java开源报表JasperReport、iReport4.5.1使用详解(一)

          最近的项目中用到了报表,所使用的工具就是JasperReport4.5.1和iReport4.5.1,JasperReport和iReport的版本一定要对应.

一、简介

        JasperReport是一个强大、灵活的开源报表生成工具,能够展示丰富的页面内容,并将之转换成PDF,HTML,或者XML格式。该库完全由Java写成,可以用于在各种Java应用程序,包括J2EE,Web应用程序中生成动态内容。

       iReport 也是开源软件,其主要作用是用来以可视化的方式设计生成JasperReport 所使用的报表格式文件,因为JasperReport 本身并未提供很好的可视化报表设计方式,ireport可视化的设计界面,支持拖拉试操作,方便、快捷的制作出各种复杂的报表。

详细的介绍,各位移步官网吧,那写的比我这详细多了。

本人使用的是最新版本4.5.1

二、安装,使用

       ①JasperReport 的安装,只需下载后解压即可。单独的一个 JasperReport Library是不能独自运行的,其实我们也不是真正意义上的安装,我们只需要把它放在classpath路径下面即可,跟其他导入的jar文件放一起。

下载地址:http://sourceforge.net/projects/jasperreports/files/jasperreports/

解压后目录如下所示

dist目录为自身的jar包文件,lib是依赖的第三方jar包文件,可根据需要选取。

比如 JfreeChart、iText、XML、JDBC 、POI等...

需要说明下的是,Linux下面的这个就有点麻烦了,JasperReport 使用的是AWT产生的报告,所以,如果Linux下面没有图形界面的话,这个就不能正常使用了。

demo下面有个example示例,可以运行了,感受下。

demo\hsqldb为演示用的数据库。

运行方式如下:

Win键+R 打开命令提示符,进入 hsqldb目录下面,运行示例数据库

 
  1.  
  2. G:\Jaspersoft\jasperreports-4.5.1\demo\hsqldb>ant runServer  
  3. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\hsqldb\build.xml  
  4.  
  5. runServer:  
  6.      [java] [Server@e0e1c6]: [Thread[main,5,main]]: checkRunning(false) entered  
  7.      [java] [Server@e0e1c6]: [Thread[main,5,main]]: checkRunning(false) exited  
  8.      [java] [Server@e0e1c6]: Startup sequence initiated from main() method  
  9.      [java] [Server@e0e1c6]: Loaded properties from [G:\Jaspersoft\jasperreports  
  10. -4.5.1\demo\hsqldb\server.properties]  
  11.      [java] [Server@e0e1c6]: Initiating startup sequence...  
  12.      [java] [Server@e0e1c6]: Server socket opened successfully in 62 ms.  
  13.      [java] [Server@e0e1c6]: Database [index=0id=0db=file:test, alias=] open  
  14. ed sucessfully in 984 ms.  
  15.      [java] [Server@e0e1c6]: Startup sequence completed in 1046 ms.  
  16.      [java] [Server@e0e1c6]: 2012-03-08 13:17:21.171 HSQLDB server 1.8.0 is onli  
  17. ne  
  18.      [java] [Server@e0e1c6]: To close normally, connect and execute SHUTDOWN SQL  
  19.  
  20.      [java] [Server@e0e1c6]: From command line, use [Ctrl]+[C] to abort abruptly  
  21.  
  22.  
  23.  

再打开一个命令提示符窗口:红色字体为输入的命令。

 

 
  1.  
  2. G:\Jaspersoft\jasperreports-4.5.1\demo\samples>cd barbecue  
  3.  
  4. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant -p  
  5. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml  
  6. Shows how barcodes could be included in reports using the Barbecue component.  
  7. Main targets:  
  8.  
  9.  clean          Deletes all the generated files.  
  10.  compile        Compiles the XML report design and produces the .jasper file.  
  11.  csv            Generates a CSV version of the report by converting the .jrprint  
  12.  file.  
  13.  decompile      Decompiles a .jasper file into a .jrxml file.  
  14.  docx           Generates a DOCX version of the report by converting the .jrprin  
  15. t file.  
  16.  fill           Fills the compiled report design with data and produces the .jrp  
  17. rint file.  
  18.  html           Generates an HTML version of the report by converting the .jrpri  
  19. nt file.  
  20.  javac          Compiles the java source files of the test application.  
  21.  jxl            Generates an XLS version of the report by converting the .jrprin  
  22. t file using the JExcelApi library.  
  23.  ods            Generates an ODS version of the report by converting the .jrprin  
  24. t file.  
  25.  odt            Generates an ODT version of the report by converting the .jrprin  
  26. t file.  
  27.  pdf            Generates a PDF version of the report by converting the .jrprint  
  28.  file.  
  29.  pptx           Generates a PPTX version of the report by converting the .jrprin  
  30. t file.  
  31.  print          Sends the report in the .jrprint file to the printer.  
  32.  rtf            Generates an RTF version of the report by converting the .jrprin  
  33. t file.  
  34.  test           Runs the sample.  
  35.  view           Launches the report viewer to preview the report stored in the .  
  36. jrprint file.  
  37.  viewDesign     Launches the design viewer to preview the compiled report design  
  38. .  
  39.  viewDesignXml  Launches the design viewer to preview the XML report design.  
  40.  viewXml        Launches the report viewer to preview the generated report store  
  41. d in an XML file.  
  42.  writeApi       Generates a .java version of the report by converting the .jrxml  
  43.  file.  
  44.  writeApiXml    Generates a .jrxml version of the report by executing the API ve  
  45. rsion of the report design.  
  46.  xhtml          Generates an XHTML version of the report by converting the .jrpr  
  47. int file.  
  48.  xls            Generates an XLS version of the report by converting the .jrprin  
  49. t file using the POI library.  
  50.  xlsx           Generates an XLSX version of the report by converting the .jrpri  
  51. nt file.  
  52.  xml            Generates an XML version of the report by converting the .jrprin  
  53. t file.  
  54.  xmlEmbed       Generates an XML version of the report by converting the .jrprin  
  55. t file.  
  56. Default target: test  
  57.  
  58. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue> 

 

 
  1. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant javac  
  2. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml  
  3.  
  4. javac:  
  5.  
  6. javac:  
  7.     [mkdir] Created dir: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue  
  8. \build\classes  
  9.     [javac] G:\Jaspersoft\jasperreports-4.5.1\demo\samples\build.xml:41: warning  
  10. : 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to  
  11.  false for repeatable builds  
  12.     [javac] Compiling 1 source file to G:\Jaspersoft\jasperreports-4.5.1\demo\sa  
  13. mples\barbecue\build\classes  
  14.  
  15. BUILD SUCCESSFUL  
  16. Total time: 6 seconds  
  17.  
  18. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant compile  
  19. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml  
  20.  
  21. compile:  
  22.  
  23. compile:  
  24.     [mkdir] Created dir: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue  
  25. \build\reports  
  26.       [jrc] Compiling 1 report design files.  
  27.       [jrc] File : G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\repor  
  28. ts\BarbecueReport.jrxml ... OK.  
  29.  
  30. BUILD SUCCESSFUL  
  31. Total time: 9 seconds  
  32.  
  33. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue> 

 

 
  1. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant fill  
  2. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml  
  3.  
  4. fill:  
  5.  
  6. fill:  
  7.      [java] Filling time : 26265  
  8.  
  9. BUILD SUCCESSFUL  
  10. Total time: 29 seconds  
  11.  
  12. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant view  
  13. Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml  
  14.  
  15. view:  
  16.  
  17. view:  
  18.  
  19. BUILD SUCCESSFUL  
  20. Total time: 40 seconds  
  21.  
  22. G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue> 

ant view 之后就可以看到报表制作的预览界面:
 

 

下面的几个命令,依次如下,就不贴了。

 
  1.  
  2. > ant viewDesign   
  3. > ant run   
  4. > dir build\reports   
  5. > ant clean  

依次

iReport 安装很简单,跟普通的软件安装没多大区别。

下载地址:http://jasperforge.org/projects/ireport

三、连接数据源

ireport安装好之后,启动界面如下:

 

软件的主界面如下:

 

 ③选择数据源

 

 JasperReport生成报表的数据源可以是数据库、xml文件、excel文件等,本文使用的是数据库。Sql Server 2000.

关于数据源的选择,可以有两种方式:如下图

 

或者下图中的QuickStart

Step1.

选择建立数据源
 

选择新建 New

选择连接方式:本文使用的是JDBC连接方式

 

Next

选择连接驱动

 

本文选择的是JTDS驱动,也可以选择微软的驱动,不过Sql Server 2000的驱动和Hibernate3.0 据说有兼容性的问题,所以本人Java开发中用到Sql Server的所有驱动均采用的是JTDS。

 

测试连接

 

 

Save即可.

注意:如果此处报错,

1.请检查数据库是否正常连接

2.检查驱动包是否正常导入。 工具-->选项 进行查看

如图所示:

 

新建一个报表

有两种方式: 1.文件-->New  2.QuickStart 中的Step2

 

右边列出的是一些默认的模板文件,这里我们选择一个空白的模板。

然后打开此模板。

保存

 

预览下打开之后的界面

 

面板介绍:

      

Designer 为设计视图
XML为生成的XML代码
Preview 为预览视图
预览 可以选择预览方式:
 

 

组件面板:

设计报表的时候要用到的一些组件,直接拖拉方式即可。关于各常用的组件介绍:

 

 

这些面板的介绍,参见下节博文。

本节内容还是比较简单的,算是一个入门,知道有这么两个软件是来设计报表的。






 本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/800135,如需转载请自行联系原作者


相关文章
|
3月前
|
设计模式 敏捷开发 Java
全网首发!Java界的四大名著之一:Java编程思想最新中文版已开源
老版《Java编程思想》(原书名《Thinking in Java》)得益作者开放深度研讨的创作方式,受到了全世界读者的追捧,被译为了十几种语言。但遗憾的是,在经历了 4 个版本的更新后,其最后一版发布于 2007 年,之后再无更新。
|
5月前
|
Java Maven
开源一套原创文本处理工具:Java+Bat脚本实现自动批量处理对账单工具
这款工具是笔者在2018年初开发完成的,时隔两载,偶然想起这款小工具,于是,决定将其开源,若有人需要做类似Java批处理实现整理文档的工具,可参考该工具逻辑思路来实现。
25 0
|
6月前
|
算法 Java 程序员
月薪3W+ 多亏发现GitHub开源的百万星Java神技-中高级核心知识解析
一提到高薪职业,大多数人最先想到的就是程序员。前几天,阿嘴正好刷到一条关于“程序员薪资”的帖子,一位网友问:想听个实话,程序员真的很容易月薪三四万吗? 这条帖子快速吸引了许多人的关注,短短几日内评论量上涨到300+,程序员的薪资待遇真的有那么好吗?大家是怎么回答的?和阿嘴一起来看看吧!
|
6月前
|
机器学习/深度学习 设计模式 人工智能
开源即巅峰!《Java程序性能优化实战》GitHub三小时标星已超34k
阿嘴又来给大家分享好书了:葛一鸣老师的 《Java程序性能优化实战》,开源版本网上暂时还没!小编会在文末附电子版免费下载方式。
|
5月前
|
设计模式 Java 数据库
持续霸榜GitHub的面试神器:字节跳动Java面试参考手册,限时开源
最近又赶上跳槽的高峰期(招聘旺季),好多读者都问我有没有面试字节的神器,我苦苦寻到了一份内部资料《2023字节跳动Java面试参考手册(第二期)》。
|
3月前
|
设计模式 Java 程序员
感动哭了!Java界的四大名著之一:Java编程思想最新中文版已开源
还记得这本书吗?是不是已经在你的桌上铺满厚厚的一层灰了?随着 Java 8 的出现,这门语言在许多地方发生了翻天覆地的变化。最新版已经出来了,在新的版本中,代码的运用和实现上与以往不尽相同。
|
3月前
|
算法 Java 程序员
阿里P8大佬终于把春招面试必备的神级Java面试手册给开源了!
先说说Java Java 作为国人编程开发语言中的 NO.1,已经占比半壁江山,选择入行做 IT 做编程开发的人,基本都把它作为首选语言,进大厂拿高薪也是大多数小伙伴们的梦想。 以前Java 岗位人才的空缺,而需求量又大,所以这种人才供不应求的现状,就是 Java 工程师的薪资待遇相对优厚的原因所在。 但是随着这个从事行业的人数逐渐增多,行业竞争也越来越大,招聘的企业和程序员们都想招聘到自己需要的人才/找到自己理想的岗位,国内大厂尤其是阿里招聘Java岗位居多,导致现在 Java 面试越来越难,内卷早就是大势所趋,万物皆可卷,卷的我们都见怪不怪了。 那么,阿里Java面试难度大吗?
|
3月前
|
JSON fastjson 数据库
字符编码导致Rapidjson(腾讯开源的json解析库)到Fastjson(阿里开发的Java json解析库)转换失败的原因分析
最近在客户端的开发的过程中,使用到了RapidJson,公司的开发是客户端和数据库端都由不同的人进行开发,我负责的客户端的逻辑开发(使用c++),开发工具同时使用了VS2017和QT的编译环境,使用QT主要是为了客户端界面开发方便,而使用了VS环境主要是维护公司开发的数据库接口库,这个库的唯一作用就是作为一个中间桥梁,使用Rapidjson将数据库接口的json数据格式解析为结构体数据,从而在客户端界面进行展示,或者接收客户端的数据,使用Rapidjson将其转换为json数据,发送给数据库接口以保存数据使用 。不太明白的可以参考我上一篇文章说明Rapidjson的使用过程-Parse解析数组
48 0
|
3月前
|
设计模式 算法 NoSQL
阿里巴巴官方上线!号称国内Java八股文天花板(终极版)首次开源
真正有意义的就业与跳槽,是要进入到一个有绝对潜力的行业或者薪资能实现爆炸式增长的。这件事不容易,但也没有想象的遥不可及,现在大环境不好,机会也不如以前多,除了让自身技术能力过硬,面试更是要好好准备! 如何准备? 除了平时的技术积累与沉淀之外,剩下的就只能背八股了(虽然工作用不到,但面试就是要问,不背是不行的)。
79 0
|
4月前
|
消息中间件 小程序 Java
暹罗点餐开源啦,一款java多门店点餐系统-连锁门店如蜜雪冰城瑞幸咖啡
暹罗点餐是一款Java餐饮点餐系统,适用于多门店的连锁品牌,对标蜜雪冰城、瑞幸咖啡。系统包含用户端、商家端、配送端以及总管理后台; * 前端使用uni-app开发,可打包部署到微信小程序、APP、H5 * Web端使用vue + Element开发 * 服务端使用java语言开发,技术栈:Spring Boot + Redis + RocketMQ + WebSocket + ElasticSearch + ELK + SpringBoot Admin
81 1
暹罗点餐开源啦,一款java多门店点餐系统-连锁门店如蜜雪冰城瑞幸咖啡