先介绍一下它们的关系:
jasperReport:一个开源的,强大灵活的报表类库,能生成html,pdf等等其它格式报表。
官网:http://jasperforge.org/projects/jasperreports
下载时把整个工程下下来。如图:
ireport:是jasperReport的可视化设计工具,使用ireport,可以更快地设计出想要的报表
下载:http://jasperforge.org/projects/ireport
注:我一开始的时候下的是最新版3.7.1,但后来运行老出问题,换了3.1版就好了
它有各个平台下的版本,windows下的下来直接安装即可。
struts2:著名的mvc框架,不多说了。
用到的jar包:
在struts2的:
说明:json那个是我个别需要加的,可以去掉
在jasperReport的:
说明:itextAsian.jar 是为了中文能正确显示的包。
平时我们做开发时,是先用ireport设计好报表,然后编译得到名为xxx.jasper的文件。然后把xxx.jasper放到工程下使用,下面是做的一个列子(使用javabean数据源):
1.运行我们的ireport,新建一个report文件;
2.设置我们javabean的classpath路径,工具,选项,classpath,addFolder,选择工程下的的classpath路径加进去;如我的:F:\ansure_g_struts2\ext_struts2_004\WebRoot\WEB-INF\classes
3.为report添加数据源,右击report的名称,选择Edit query,javabean datasource,输入你的javabean名称,把其属性列表读取出来,把要打印的属性加到下面去,点确定。如图:
4.这样我们就可以在左侧的树形目录下看到Fields下有相关的属性,把他们拖到报表的detail 区域下,如图:
5.点击编译,生成xxx.jasper文件。
6.在你的web工程的根目录下建立一个文件夹jasper,把xxx.jasper文件放进该目录下
7.配置struts.xml,具体请看源文件。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="test" extends="struts-default,jasperreports-default"> <action name="toHtml" class="com.ansure.action.TestAction" method="execute"> <result name="success" type="jasper"> <param name="location">/jasper/report2.jasper</param> <param name="dataSource">userList</param> <param name="format">HTML</param> </result> <result name="error"> /info_error.jsp </result> </action> <action name="toPdf" class="com.ansure.action.TestAction" method="execute"> <result name="success" type="jasper"> <param name="location">/jasper/report2.jasper</param> <param name="dataSource">userList</param> <param name="format">PDF</param> </result> <result name="error"> /info_error.jsp </result> </action> <action name="toXml" class="com.ansure.action.TestAction" method="execute"> <result name="success" type="jasper"> <param name="location">/jasper/report2.jasper</param> <param name="dataSource">userList</param> <param name="format">XML</param> </result> <result name="error"> /info_error.jsp </result> </action> <action name="toXls" class="com.ansure.action.TestAction" method="execute"> <result name="success" type="jasper"> <param name="location">/jasper/report2.jasper</param> <param name="dataSource">userList</param> <param name="format">XLS</param> </result> <result name="error"> /info_error.jsp </result> </action> </package> </struts>
javaBean
package com.ansure.moudle; public class User { private int u_id; private String u_name; private String u_pwd; private String u_email; public User() { } public User(int id, String name, String pwd, String email) { this.u_id = id; this.u_name = name; this.u_pwd = pwd; this.u_email = email; } public int getU_id() { return u_id; } public void setU_id(int uId) { u_id = uId; } public String getU_email() { return u_email; } public void setU_email(String email) { this.u_email = email; } public String getU_name() { return u_name; } public void setU_name(String uName) { u_name = uName; } public String getU_pwd() { return u_pwd; } public void setU_pwd(String uPwd) { u_pwd = uPwd; } }
Action
package com.ansure.action; import java.util.List; import com.ansure.moudle.User; import com.ansure.service.UserService; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<User> userList; UserService userService = new UserService(); public String execute() { try { userList = userService.selec_all(); //数据Lsit } catch (Exception e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } }
视图页面index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JasperReport</title> </head> <body> <a href="test/toHtml.action">toHTML</a> <br><a href="test/toPdf.action">toPDF</a> <br><a href="test/toXml.action">toXML</a> <br><a href="test/toXls.action">toXLS</a> </body> </html>
生成报表效果图:
相关资料:http://neil-jh.iteye.com/blog/235834
http://blog.csdn.net/nopain_nogain/archive/2009/06/27/4302437.aspx
http://hi.baidu.com/cool%5Flijia/blog/category/ireport%B1%A8%B1%ED