您的位置:首页 > 编程语言 > ASP

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

2012-03-08 11:06 405 查看
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目录下面,运行示例数据库
G:\Jaspersoft\jasperreports-4.5.1\demo\hsqldb>ant runServer
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\hsqldb\build.xml

runServer:
[java] [Server@e0e1c6]: [Thread[main,5,main]]: checkRunning(false) entered
[java] [Server@e0e1c6]: [Thread[main,5,main]]: checkRunning(false) exited
[java] [Server@e0e1c6]: Startup sequence initiated from main() method
[java] [Server@e0e1c6]: Loaded properties from [G:\Jaspersoft\jasperreports
-4.5.1\demo\hsqldb\server.properties]
[java] [Server@e0e1c6]: Initiating startup sequence...
[java] [Server@e0e1c6]: Server socket opened successfully in 62 ms.
[java] [Server@e0e1c6]: Database [index=0, id=0, db=file:test, alias=] open
ed sucessfully in 984 ms.
[java] [Server@e0e1c6]: Startup sequence completed in 1046 ms.
[java] [Server@e0e1c6]: 2012-03-08 13:17:21.171 HSQLDB server 1.8.0 is onli
ne
[java] [Server@e0e1c6]: To close normally, connect and execute SHUTDOWN SQL

[java] [Server@e0e1c6]: From command line, use [Ctrl]+[C] to abort abruptly

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

G:\Jaspersoft\jasperreports-4.5.1\demo\samples>cd barbecue

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant -p
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml
Shows how barcodes could be included in reports using the Barbecue component.
Main targets:

clean          Deletes all the generated files.
compile        Compiles the XML report design and produces the .jasper file.
csv            Generates a CSV version of the report by converting the .jrprint
file.
decompile      Decompiles a .jasper file into a .jrxml file.
docx           Generates a DOCX version of the report by converting the .jrprin
t file.
fill           Fills the compiled report design with data and produces the .jrp
rint file.
html           Generates an HTML version of the report by converting the .jrpri
nt file.
javac          Compiles the java source files of the test application.
jxl            Generates an XLS version of the report by converting the .jrprin
t file using the JExcelApi library.
ods            Generates an ODS version of the report by converting the .jrprin
t file.
odt            Generates an ODT version of the report by converting the .jrprin
t file.
pdf            Generates a PDF version of the report by converting the .jrprint
file.
pptx           Generates a PPTX version of the report by converting the .jrprin
t file.
print          Sends the report in the .jrprint file to the printer.
rtf            Generates an RTF version of the report by converting the .jrprin
t file.
test           Runs the sample.
view           Launches the report viewer to preview the report stored in the .
jrprint file.
viewDesign     Launches the design viewer to preview the compiled report design
.
viewDesignXml  Launches the design viewer to preview the XML report design.
viewXml        Launches the report viewer to preview the generated report store
d in an XML file.
writeApi       Generates a .java version of the report by converting the .jrxml
file.
writeApiXml    Generates a .jrxml version of the report by executing the API ve
rsion of the report design.
xhtml          Generates an XHTML version of the report by converting the .jrpr
int file.
xls            Generates an XLS version of the report by converting the .jrprin
t file using the POI library.
xlsx           Generates an XLSX version of the report by converting the .jrpri
nt file.
xml            Generates an XML version of the report by converting the .jrprin
t file.
xmlEmbed       Generates an XML version of the report by converting the .jrprin
t file.
Default target: test

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>


G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant javac 
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml

javac:

javac:
[mkdir] Created dir: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue
\build\classes
[javac] G:\Jaspersoft\jasperreports-4.5.1\demo\samples\build.xml:41: warning
: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to
false for repeatable builds
[javac] Compiling 1 source file to G:\Jaspersoft\jasperreports-4.5.1\demo\sa
mples\barbecue\build\classes

BUILD SUCCESSFUL
Total time: 6 seconds

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant compile 
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml

compile:

compile:
[mkdir] Created dir: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue
\build\reports
[jrc] Compiling 1 report design files.
[jrc] File : G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\repor
ts\BarbecueReport.jrxml ... OK.

BUILD SUCCESSFUL
Total time: 9 seconds

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>


G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant fill 
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml

fill:

fill:
[java] Filling time : 26265

BUILD SUCCESSFUL
Total time: 29 seconds

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>ant view 
Buildfile: G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue\build.xml

view:

view:

BUILD SUCCESSFUL
Total time: 40 seconds

G:\Jaspersoft\jasperreports-4.5.1\demo\samples\barbecue>

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





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

> ant viewDesign
> ant run
> dir build\reports
> 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 为预览视图
预览 可以选择预览方式:





组件面板:

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









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

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

本文出自 “幽灵柯南的技术blog” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: