posts - 84, comments - 54, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Web application 中使用Crystal Report(一)

Posted on 2007-04-11 23:26 马达+斯加 阅读(1703) 评论(8)  编辑  收藏 所属分类: Java Report
最近的项目中使用了Crystal Report作为报表工具,与Jasper Report不同,在报表的灵活性和开发速度上要胜出很多,一旦用上就不愿意换其他了。
在Web application中使用CR,主要可以通过两种方案实施:
一种是将rpt文件嵌入application中,该方案实施简单、快速,最重要的不受license限制,是普通项目开发的绝佳选择。我们可以直接去Business Objects上面下载一个已经集成了CR开发插件的Eclipse,或者只下载插件也可以。利用该插件中的开发向导,可以非常容易的进行开发了。
生成HTML报表也是相当的简单,我们可以通过CR提供的标签库生成:
<%@ taglib uri="/WEB-INF/tld/crystal-tags-reportviewer.tld" prefix="crviewer" %>

<crviewer:viewer 
isOwnPage="true" 
displayGroupTree
="false"
printMode
="ActiveX"
allowDrillDown
="false"
hyperlinkTarget
="false"
displayToolbarViewList
="false"
displayToolbarFindButton
="false"
displayToolbarToggleTreeButton
="false"
displayToolbarCrystalLogo
="false"
reportSourceType
="reportingComponent" 
viewerName
="CrystalReport1-viewer" 
reportSourceVar
="CrystalReport1">
<crviewer:report reportName="Test.rpt" />
</crviewer:viewer>
也可以通过自己写代码读取report source,然后作为一个javabean供表现层使用:
    /**
     * Retrieve embedded report source by the specified report name.
     * 
     * 
@param reportName
     * 
@return
     
*/

    
private static IReportSource getEmbeddedReport(String reportName){
        IReportSource reportSource 
= null;
        String report 
= CRConstant.EMBEDDED_RPT_FOLDER + reportName + ".rpt";
        ReportClientDocument reportClientDoc 
= new ReportClientDocument();
        
try {
            reportClientDoc.open(report, 
0);
        }
 catch (ReportSDKException e) {
                            
            log.warn(
"Cant get embedded report source");
            log.error(e.getMessage());
        }

        reportSource 
= reportClientDoc.getReportSource();
        
return reportSource;
    }

非标签方式生成HTML报表
<%@ page 
language
="java"
contentType
="text/html; charset=UTF-8"
pageEncoding
="UTF-8"

%>
<%@ page import="com.crystaldecisions.reports.reportengineinterface.*"%>
<%@ page import="com.crystaldecisions.report.web.viewer.*" %>
<%
 CrystalReportViewer viewer 
= (CrystalReportViewer)session.getAttribute("crViewer");
viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), 
null); 
 
%>

另外一种是集成Crystal Enterprise Server进行报表开发。所有的rpt都在server上,application 可以通过包括Enterprise account,LDAP,Windows NT(如果是Window服务器)等多种方式登陆Server读取报表。该方案中rpt可以提供多个应用使用,并且方便管理及二次开发,但是license非常贵,因此,主要是应用在大型项目中。
其中最简单的便是使用Enterprise Account方式登录了:
    /**
     * Loging Crystal Enterprise server
     *
     
*/

    
private void createEnterpriseSession() throws SDKException{
        String cms 
= CRConstant.CMS;
        String userName 
= CRConstant.AUTH_USERNAME;
        String password 
= CRConstant.AUTH_PASSWORD;
        String authMode 
= CRConstant.AUTH_MODE;
        
try {
            
            ISessionMgr sessionMgr 
= CrystalEnterprise.getSessionMgr();
            iEnterpriseSession 
= sessionMgr.logon(userName, password, cms, authMode);
            log.info(
"Got Enterprise session successfully");
            
        }
 catch (SDKException e) 
            log.warn(
"Cant log on CMS:\nCMS:" + cms + "\nUserID:"
                    
+ userName + "\nPassword:" + password + "\nAuthMode:"
                    
+ authMode);
            
throw e;
        }
   
        
    }

但是,这里特别需要注意的是,CMS的值必须是Enterprise server的名称,而不能使用IP地址。
在此方案中,可以使用一个iFrame直接将Server中生成的报表包含进来(CR enterprise portlet就是这样干的)。

评论

# re: Web application 中使用Crystal Report(一)[未登录]  回复  更多评论   

2007-05-04 17:57 by 我们走在JAVA的光明大道上
很好的经验!谢谢分享

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 17:37 by Alan.xiao
通过eclipse的插件打开一个用crystal reporst 10做的报表,提示要连接数据库,因此通过它的数据库资源管理器连接了sql server,结果发现只能看到数据库中的表,存储过程看不到,所以它把那张报表里面的数据按名字对应表里面去了,而实际上本应对应一个存储过程的,所以显示数据库中没有该表。请问您,这是怎么回事,该如何解决

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 17:48 by 回楼上
好久没用了,记得好像cr里面只能看到table,view。碰到很复杂的报表,我们一般是建一个working table,很少用存储过程。
另外想问一下,为什么存储过程的名字会和table一样呢?一般会避免这种情况才是。

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 17:50 by Alan.xiao
如果以上描述您不是很理解,那我换个说法:

报表已经存在,我目前要做的是在jsp中生成,而这样做需要输入参数,输入参数就需要“报表源”,得到报表源就需要“jdbc数据源”,而原来的的报表是通过DSN连接的数据库,所以不得已重新配置,结果配好数据源后发现没法读取数据库中的存储过程。

(要是能截图就好了)

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 17:52 by Alan.xiao
只有存储过程,没有改名字的table,但因为那个插件读不到存储过程,就把那个当table了,而该table又不存在,所以就报错啦

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 18:00 by 回楼上
没有做过这种case,如果没记错的话,使用哪个数据源不是在rpt中定义的吗?

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 18:05 by Alan.xiao
是啊,是在rpt中定义的,但帮组文档中有这样一句话:

要使用 Java Reporting Component 创建报表源,必需指定要查看或导出的报表所在的位置。将使用相对或绝对路径指定报表位置。而且,为使 Java Reporting Component 正确地为报表检索数据,必须通过 JNDI 正确指定报表的数据源,或必须依据 JDBC 数据源设计报表。

# re: Web application 中使用Crystal Report(一)  回复  更多评论   

2007-12-20 18:08 by 回楼上
或者,你可以尝试用JDBC数据源新建一个使用到stored proc的非常简单的报表,看看在你的application里面能否正确运行。

只有注册用户登录后才能发表评论。


网站导航: