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 为预览视图
预览 可以选择预览方式:
 

 

组件面板:

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

 

 

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值