最大的网站源码资源下载站,
最近为客户开发一个mis系统,java技术,b/s结构.客户要求系统的报表采用水晶报表.
怎样在jsp中实现水晶报表呢?找遍了所有的资料,水晶报表的web实现倒是很多,但都讲的是微软的.net方案.jsp中如何实现水晶报表的动态显示呢?
经过一段时间的痛苦摸索,现在把我的方案介绍给大家,希望各位大哥批评指正我的问题:
需要动态的显示水晶报表,并且要根据我传递的一个参数(bh)的值动态的显示数据.并且要可以把水晶报表导出为word,excel,pdf等格式
我的解决方案:
一:下载 crystal reports 10 for bea weblogic workshop
二:安装crystal reports 10 for bea weblogic workshop;
三:如果安装成功,则在c:/program files/common files/crystal decisions/2.5/bin目录下,可以找到crdb_javaserver.ini文件.该文件很重要,里面的内容
关系到水晶报表如何访问数据库.我的水晶报表采用jdbc的方式访问oracle数据库,配置如下:
[common]
path = c:/bea/jdk141_05/bin
classpath = c:/projects/drivers/jdbc/classes12.jar;
c:/projects/drivers/jdbc/msbase.jar;
c:/projects/drivers/jdbc/mssqlserver.jar;
c:/projects/drivers/jdbc/msutil.jar;
c:/projects/drivers/jdbc/common.jar;
c:/projects/drivers/jdbc/db2fs.jar;
c:/projects/drivers/jdbc/db2java.zip;
c:/projects/drivers/jdbc/db2jcc.jar;
c:/projects/drivers/jdbc/weblogic.jar;
d:/progra~1/ibm/websph~1/applic~1/v5.1.1/runtimes/base_v51/lib/naming.jar;
d:/progra~1/ibm/websph~1/applic~1/v5.1.1/runtimes/base_v51/lib/namingclient.jar;
d:/progra~1/ibm/websph~1/applic~1/v5.1.1/runtimes/base_v51/lib/namingserver.jar;
d:/progra~1/ibm/websph~1/applic~1/v5.1.1/runtimes/base_v51/lib/cmimpl.jar;
d:/progra~1/ibm/websph~1/applic~1/v5.1.1/runtimes/base_v51/lib/j2cimpl.jar;
c:/projects/drivers/jdbc/weblogic.jar;
c:/program files/common files/crystal decisions/2.5/bin/crdbjavaserver.jar;
c:/bea/weblogic81/server/lib/ojdbc14.jar
iorfilelocation = ${temp}
javaservertimeout = 1800
jvmmaxheap = 64000000
jvmminheap = 32000000
[crdb_jdbc]
cacherowsetsize = 100
jdbcurl = jdbc:oracle:thin:@192.168.1.99:1521:pwsc
jndiurl =
jdbcusername = testuser
jdbcdrivername = oracle.jdbc.driver.oracledriver
jndiusername = weblogic
jndiconnectionfactory =
jndiinitcontext = /
genericjdbcdriverbehavior = db2
[crdb_xml]
prereadnbyte = 5000
cacherowsetsize = 100
xmllocalurl =
schemalocalurl =
xmlhttpurl =
schemahttpurl =
usetempfile = true
以上是配置文件,需要说明的是:
1:由于我采用的是jdbc的方式访问数据库,所以classpath中一定要把c:/bea/weblogic81/server/lib/ojdbc14.jar加进去,它是jdbc的驱动
2:[crdb_jdbc]中的:jdbcurl = jdbc:oracle:thin:@192.168.1.99:1521:pwsc 是我要访问的数据库名称.jdbcusername = testuser是设置用户名,
jdbcdrivername = oracle.jdbc.driver.oracledriver是jdbc驱动名称
四:在水晶报表编辑器中编辑我要用的报表.由于我需要动态的给报表传递参数,所以我在报表中设置了一个参数字段,水晶报表根据这个参数字段,就可以显示相应的
数据,参数字段命名为bh,在公式编辑器中编写如下代码:
if {?bh}<>"" then
{gg_bpbj.jlbh}={?bh}
else
{gg_bpbj.jlbh}="1" or {gg_bpbj.jlbh}<>"1"
以上代码的意思是:如果bh参数不为空,则显示{gg_bpbj.jlbh}等于参数bh的记录,否则显示出所有的记录
(大家可能觉得{gg_bpbj.jlbh}="1" or {gg_bpbj.jlbh}<>"1"很可笑,是啊,我开始是写成"true"了,在水晶报表编辑器中可以很好的运行,但方到web上显示就有
问题,万般无奈,只有如此了,:( )
五:在weblogic workshop中新建一个web项目,用右键选择该项目文件夹,选择"新建",选择"crystal report",则在该web项目根目录下自动新建了一个
crystalreportviewers10目录,里面有显示水晶报表需要的页面元素系统自动添加了开发水晶报表需要的jar包,共有13个jar文件(位于web_inf/lib目录下),
并增加了一些水晶报表专用的标签(在web_inf/src下)
六:需要特别注意的是:在web_inf下的web.xml文件也随之改动,改动后的web.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- the web.xml file is a configuration file used to control the behavior of weblogic server.
in most cases, you will not need to modify this file. for more information on web.xml, please
consult the web.xml deployment descriptor elements chapter of the "developing weblogic server
applications" documentation on edocs.bea.com. -->
<web-app>
<display-name>workshop application</display-name>
<context-param>
<param-name>weblogic.httpd.inputcharset./*</param-name>
<param-value>gbk</param-value>
</context-param>
<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/webmis/crystalreportviewers10</param-value>
</context-param>
<filter>
<filter-name>pageflowjspfilter</filter-name>
<filter-class>com.bea.wlw.netui.pageflow.pageflowjspfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pageflowjspfilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.bea.wlw.runtime.core.servlet.webappcontextlistener</listener-class>
</listener>
<!-- standard action servlet configuration (with debugging) -->
<servlet>
<servlet-name>messageservlet</servlet-name>
<servlet-class>com.infoearth.servlets.gettaskservlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.bea.wlw.netui.pageflow.dynamicsubappactionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/jpf-struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/-global</param-name>
<param-value>/web-inf/jpf-struts-config--global.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>drawimage</servlet-name>
<servlet-class>com.infoearth.drawimage.drawimageservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>drawimage</servlet-name>
<url-pattern>drawimage</url-pattern>
</servlet-mapping>
<!-- struts action servlet mappings -->
<!-- note that because struts takes the *last* mapping here as the extension to add to
actions posted from forms, we must have *.do come after *.jpf. -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.jpf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>messageservlet</servlet-name>
<url-pattern>/messageservlet</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>login/logincontroller.jpf</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<!-- define the netui tag library tlds -->
<taglib>
<taglib-uri>netui-tags-html.tld</taglib-uri>
<taglib-location>/web-inf/netui-tags-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>netui-tags-databinding.tld</taglib-uri>
<taglib-location>/web-inf/netui-tags-databinding.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>netui-tags-template.tld</taglib-uri>
<taglib-location>/web-inf/netui-tags-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/crystal-tags-reportviewer.tld</taglib-uri>
<taglib-location>/web-inf/crystal-tags-reportviewer.tld</taglib-location>
</taglib>
</web-app>
其中:
<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/webmis/crystalreportviewers10</param-value>
</context-param>
很关键,它设置了水晶报表专用的crystalreportviewers10路径(第五步中自动建的crystalreportviewers10目录),如果设置错误,将来点击水晶报表自带的
导出和打印按钮是就会报页面错误,并且水晶报表自己的各种翻页,导出等图标也无法正常显示.
七:把编辑好的水晶报表(后缀命为rpt)放在一个目录下(我自己建了一个report目录,专门存放报表文件)
八:可以写程序了:
1.编写reportinit类.作用:用于传递参数bh.这个类很关键,负责把java的参数值传递到水晶报表文件.
2.在页面流中增加showrpt操作:其实就是实例化一个reportinit类.
3.编写viewer.jsp文件用来动态显示水晶报表.
后面有我的源代码,请大家批评指正.
存在的问题:
1:水晶报表导出成pdf文件时有问题:汉字显示为乱码,不知何故?此问题困扰我几个月了,没法解决
2:无法把打印/导出页面汉化:在crystalreportviewers10/js目录下有一系列strings命名的js文件,我猜测是用来国际化页面的,可是我修改
strings_zh,js,strings_en.js,没有什么效果.
3:在浏览器中显示水晶报表后,关闭显示页面,该水晶报表依然和数据库连接,(可由select * from v$session 查看oracle的连接数).如果在
viewer.jsp中加入断开连接的语句:
viewer.dispose();
rptsource.dispose();
则只能显示一页报表.并且打印和导出不能工作.
以上问题困扰我很久,虽然并不影响使用,但心中还是不是很爽.希望高手指点.
源程序:
/*
* create date: 2004-8-14 11:06
* create by: 李春雷
* purpose:设置水晶报表的查询参数(目前支持7个参数的查询,有用的参数为编号)
*/
package com.infoearth.report;
import com.crystaldecisions.report.web.viewer.*;
import com.crystaldecisions.sdk.occa.report.data.*;
import com.crystaldecisions.reports.reportengineinterface.jpereportsourcefactory;
import com.crystaldecisions.sdk.occa.report.reportsource.ireportsourcefactory2;
import com.crystaldecisions.sdk.occa.report.reportsource.ireportsource;
public class reportinit
{
private ireportsource rptsource;
private fields fields;
public reportinit(){
rptsource = null;
fields = null;
}
public ireportsource getrptsource(){
return rptsource;
}
public fields getfields(){
return fields;
}
public boolean setreport(string fname,string rptid){
//获得查询条件
string filename = fname + ".rpt";
string bh = rptid;
string path = "report/"+filename; //报表文件路径
try{
ireportsourcefactory2 rsf = new jpereportsourcefactory();
java.util.locale localetest = java.util.locale.china;
rptsource = (ireportsource)rsf.createreportsource(path,localetest);
fields = new fields();
parameterfield pfield1 = new parameterfield(); //编号
parameterfield pfield2 = new parameterfield(); //单位
parameterfield pfield3 = new parameterfield(); //人
parameterfield pfield5 = new parameterfield(); //field5
parameterfield pfield6 = new parameterfield(); //field6
parameterfield pfield4_kssj = new parameterfield(); //开始时间
parameterfield pfield4_jssj = new parameterfield(); //结束时间
values vals1 = new values();
values vals2 = new values();
values vals3 = new values();
values vals5 = new values();
values vals6 = new values();
values vals4_kssj = new values();
values vals4_jssj = new values();
parameterfielddiscretevalue pfielddv1 = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv2 = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv3 = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv5 = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv6 = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv4_kssj = new parameterfielddiscretevalue();
parameterfielddiscretevalue pfielddv4_jssj = new parameterfielddiscretevalue();
//设置第一个参数:编号
pfield1.setreportname("");
pfield1.setname("bh");
pfielddv1.setvalue(bh);
pfielddv1.setdescription("编号");
vals1.add(pfielddv1);
pfield1.setcurrentvalues(vals1);
//设置第二个参数:单位部门
pfield2.setreportname("");
pfield2.setname("dw");
pfielddv2.setvalue("");
pfielddv2.setdescription("发单部门");
vals2.add(pfielddv2);
pfield2.setcurrentvalues(vals2);
//设置第三个参数:人
pfield3.setreportname("");
pfield3.setname("ren");
pfielddv3.setvalue("");
pfielddv3.setdescription("申请人、下达人、操作人、负责人等");
vals3.add(pfielddv3);
pfield3.setcurrentvalues(vals3);
//设置第五个查询字段
pfield5.setreportname("");
pfield5.setname("field5");
pfielddv5.setvalue("");
pfielddv5.setdescription("查询字段5:任务来源线路名称等");
vals5.add(pfielddv5);
pfield5.setcurrentvalues(vals5);
//设置第六个查询字段
pfield6.setreportname("");
pfield6.setname("field6");
pfielddv6.setvalue("");
pfielddv6.setdescription("查询字段6:更改原因、工作内容等");
vals6.add(pfielddv6);
pfield6.setcurrentvalues(vals6);
//设置开始时间
pfield4_kssj.setreportname("");
pfield4_kssj.setname("kssj");
pfielddv4_kssj.setvalue("");
pfielddv4_kssj.setdescription("开始时间");
vals4_kssj.add(pfielddv4_kssj);
pfield4_kssj.setcurrentvalues(vals4_kssj);
//设置结束时间
pfield4_jssj.setreportname("");
pfield4_jssj.setname("jssj");
pfielddv4_jssj.setvalue("");
pfielddv4_jssj.setdescription("结束时间");
vals4_jssj.add(pfielddv4_jssj);
pfield4_jssj.setcurrentvalues(vals4_jssj);
//参数字段和报表关联
fields.add(pfield1);
fields.add(pfield2);
fields.add(pfield3);
fields.add(pfield5);
fields.add(pfield6);
fields.add(pfield4_kssj);
fields.add(pfield4_jssj);
return true;
}
catch(exception e){
system.out.println("class:reportinit错误:"+e);
return false;
}
}
}
//在页面流中增加如下操作:
/**
* @jpf:action
* @jpf:forward name="false" path="error.jsp"
* @jpf:forward name="success" path="viewer.jsp"
*/
protected forward showrpt()
{
string rptid = this.getrequest().getparameter("rptid");
string rptbh = this.getrequest().getparameter("rptbh");
string rptname = dbctrl.getrptcnname(rptid);
if ((rptname!=null) && (rptbh!=null)) {
reportinit rpt = new reportinit();
boolean result = rpt.setreport(rptname, rptbh);
if(result==true) {
fields fields = rpt.getfields();
ireportsource rptsource = rpt.getrptsource();
this.getsession().setattribute("fields", fields);
this.getsession().setattribute("rptsource", rptsource);
return new forward("success");
} else {
return new forward("false");
}
}
return new forward("false");
}
//viewer.jsp用来动态显示水晶报表.viewer.jsp的内容如下:
<%@ page language="java" contenttype="text/html;charset=utf-8"%>
<%@ page import= "com.crystaldecisions.report.web.viewer.*,
com.crystaldecisions.sdk.occa.report.data.*,
com.crystaldecisions.reports.reportengineinterface.jpereportsourcefactory,
com.crystaldecisions.sdk.occa.report.reportsource.ireportsourcefactory2,
com.crystaldecisions.sdk.occa.report.reportsource.ireportsource"%>
<%@ taglib uri="netui-tags-html.tld" prefix="netui"%>
<%
//fields rptsource
//string name = (string)request.getattribute("nametest");
//system.out.println("name="+name);
%>
<!-- ../crystalreportviewers10/css/default.css -->
<link rel="stylesheet" type="text/css" href="../crystalreportviewers10/css/default.css">
<%
ireportsource rptsource = (ireportsource)session.getattribute("rptsource");
fields fields = (fields) session.getattribute("fields");
connectioninfos conninfos = new connectioninfos();
iconnectioninfo conninfo1 = new connectioninfo();
conninfo1.setusername("testuser");
conninfo1.setpassword("test");
conninfos.add(conninfo1);
crystalreportviewer viewer = new crystalreportviewer();
viewer.setreportsource(rptsource);
viewer.setdatabaselogoninfos(conninfos);
viewer.setparameterfields(fields);
viewer.setenableparameterprompt(true);
viewer.setownpage(true);
viewer.setownform(true);
viewer.setprintmode(crprintmode.activex);
viewer.sethasexportbutton(true);
viewer.sethasprintbutton(true);
viewer.sethaslogo(false);
viewer.processhttprequest(request, response,getservletconfig().getservletcontext(), out);
viewer.refresh();
//viewer.dispose();
//rptsource.dispose();
%>
新闻热点
疑难解答