JAVA水晶报表从环境搭建到创建动态水晶报表

首先第一步贴上项目截图(少了一个rpt_report文件夹,因为是网上的所以这里没加,各位可以加下):声明该项目是网上下载。(比较简单,纯属偷懒,有现成环境)。可以自己修改,需要注意的就是蓝色标记的地方。报表存放的位置可以根据配置文件配置存放,下面有介绍。

第一:crystalreportviewers11这个文件夹可以通过官方网站中软件中获取,软件名称为(CR2008_SP1_55225)获取的方法是安装软件,软件会提供java显示水晶报表所需的所有文件,安装后的目录为:

这里是crystalreportviewers12,版本比我这里的高,我用的是网上的为crystalreportviewers11版本的。安装后就可以用这个制作水晶报表了。

第二:其中web.xml需要注意的地方就是:添加如下代码

  1.         <display-name>SampleWeb</display-name> 
  2. <context-param> 
  3.     <param-name>crystal_image_uri</param-name> 
  4.     <param-value>/crystalreportviewers11</param-value> 
  5. </context-param> 
  6. <context-param> 
  7.     <param-name>crystal_image_use_relative</param-name> 
  8.     <param-value>webapp</param-value> 
  9. </context-param> 
  10. <jsp-config> 
  11.     <taglib> 
  12.         <taglib-uri>/crystal-tags-reportviewer.tld</taglib-uri> 
  13.         <taglib-location> 
  14.             /WEB-INF/crystal-tags-reportviewer.tld 
  15.         </taglib-location> 
  16.     </taglib> 
  17. </jsp-config> 
         <display-name>SampleWeb</display-name>
	<context-param>
		<param-name>crystal_image_uri</param-name>
		<param-value>/crystalreportviewers11</param-value>
	</context-param>
	<context-param>
		<param-name>crystal_image_use_relative</param-name>
		<param-value>webapp</param-value>
	</context-param>
	<jsp-config>
		<taglib>
			<taglib-uri>/crystal-tags-reportviewer.tld</taglib-uri>
			<taglib-location>
				/WEB-INF/crystal-tags-reportviewer.tld
			</taglib-location>
		</taglib>
	</jsp-config>

crystal-tags-reportviewer.tld上面截图没有不好意思。可以自己添加下


关于不同版本的水晶报表web.xml的配置可以参考http://blog.sina.com.cn/s/blog_6ff49b9d01014hvr.html

第三:CRConfig.xml配置如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <CrystalReportEngine-configuration> 
  3.     <reportlocation>../../rpt_report</reportlocation> 
  4.     <timeout>10</timeout> 
  5.     <keycode>AV864-01CS00G-0ZG518J</keycode> 
  6. </CrystalReportEngine-configuration> 
<?xml version="1.0" encoding="utf-8"?>
<CrystalReportEngine-configuration>
	<reportlocation>../../rpt_report</reportlocation>
	<timeout>10</timeout>
	<keycode>AV864-01CS00G-0ZG518J</keycode>
</CrystalReportEngine-configuration>


具体<reportlocation>../../rpt_report</reportlocation>的含义自己可以百度,我在这里说下我配置的意思是,水晶报表加载的报表文件也就是rpt文件的路径在WebRoot中的rpt_report文件夹中若配置成<reportlocation>../..</reportlocation>就是说明加载的rpt文件在webroot下,具体rpt文件放在哪里可以自己灵活配置。

完成如上步骤可以算是水晶报表在myeclipse中的环境算是搭建完成。

下面是显示最简单的报表(非动态数据)

basic.jsp

  1. <%@ page language="java" pageEncoding="gb2312"%> 
  2. <%@ page import="com.crystaldecisions.reports.reportengineinterface.*"%> 
  3. <%@ page import="com.crystaldecisions.report.web.viewer.*" %> 
  4. <% 
  5.      CrystalReportViewer crv = new CrystalReportViewer(); 
  6.      JPEReportSourceFactory jrsf = new JPEReportSourceFactory(); 
  7.      crv.setReportSource(jrsf.createReportSource("报表2.rpt",request.getLocale())); 
  8.      crv.processHttpRequest(request,response,application,null); 
  9. %> 
<%@ page language="java" pageEncoding="gb2312"%>
<%@ page import="com.crystaldecisions.reports.reportengineinterface.*"%>
<%@ page import="com.crystaldecisions.report.web.viewer.*" %>
<%
	 CrystalReportViewer crv = new CrystalReportViewer();
	 JPEReportSourceFactory jrsf = new JPEReportSourceFactory();
	 crv.setReportSource(jrsf.createReportSource("报表2.rpt",request.getLocale()));
	 crv.processHttpRequest(request,response,application,null);
 %>

该jsp放在webroot下就可以了。我们这里加载的是“报表2.rpt”文件,该文件放在WebRoot中的rpt_report文件夹中,也就是刚才我们配置的路径。

若报表能够显示那说明我们从头到现在都没有问题,其中报表自己制作。至于如何制作可以参考

http://topic.csdn.net/u/20091202/20/fe299b0b-467d-4b20-aeb0-262f6492eaa4.html?25378 应该容易看懂并且掌握简单的制作。

下面是介绍后台数据显示在rpt文件中,建议各位先看下http://blog.163.com/huang_ying_lu/blog/static/269998320088108233237/楼主写的。按照里面的介绍然后自己写的话应该没有问题。

关键代码是

  1. 第一获取数据源大家应该没有什么问题。 
第一获取数据源大家应该没有什么问题。
  1. /** *//**
  2.       *    连接数据库,通过sql查询语句进行查询,返回结果集
  3.      */ 
  4.     private static ResultSet getResultSetFromQuery(String query, int scrollType) 
  5.         throws SQLException, ClassNotFoundException ...{ 
  6.          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
  7.         final String DBUSERNAME = "username"
  8.         final String DBPASSWORD = "password"
  9.         final String CONNECTION_URL = "jdbc:microsoft:sqlserver://localhost:1433;database=dname"
  10.          
  11.          java.sql.Connection connection = DriverManager.getConnection(CONNECTION_URL, DBUSERNAME, DBPASSWORD);  
  12.          Statement statement = connection.createStatement(scrollType, ResultSet.CONCUR_READ_ONLY); 
  13.          
  14.         return statement.executeQuery(query); 
  15.  
  16.      } 
/** *//**
      *    连接数据库,通过sql查询语句进行查询,返回结果集
     */
    private static ResultSet getResultSetFromQuery(String query, int scrollType)
        throws SQLException, ClassNotFoundException ...{
         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        final String DBUSERNAME = "username";
        final String DBPASSWORD = "password";
        final String CONNECTION_URL = "jdbc:microsoft:sqlserver://localhost:1433;database=dname";
        
         java.sql.Connection connection = DriverManager.getConnection(CONNECTION_URL, DBUSERNAME, DBPASSWORD); 
         Statement statement = connection.createStatement(scrollType, ResultSet.CONCUR_READ_ONLY);
        
        return statement.executeQuery(query);

     }
  1. 将查询出来的数据放到报表源中也就是reportClientDoc.getDatabaseController().setDataSource(resultSet,tableAlias, "resultsetTable"); 
将查询出来的数据放到报表源中也就是reportClientDoc.getDatabaseController().setDataSource(resultSet,tableAlias, "resultsetTable");
  1. /** *//**
  2.   * 通过sql语句过滤报表数据,在.net就不用怎么惨了
  3. */ 
  4. public boolean isReportSourceInSession(String session_name,HttpSession session) throws ReportSDKException, SQLException, ClassNotFoundException...{ 
  5.     boolean flag=false
  6.     try ...{ 
  7.         //打开水晶报表 
  8.          ReportClientDocument reportClientDoc = new ReportClientDocument(); 
  9.          reportClientDoc.open(REPORT_NAME, 0); 
  10.        <span style="color: rgb(255, 0, 0);"> //sql查询语句,返回的字段数必须跟报表里面的一样,不能多也不能少,并且字段的类型要跟报表的一样,其他不管是什么数据都可以   
  11.           //from 表这里要填完整,如数据库名.dbo.数据库表,最好做个别名 
  12. an>             String query = "select tt.test_1,tt.test_2,tt.test_3,tt.test_4 from dname.dbo.test tt"
  13.          
  14.          ResultSet resultSet = this.getResultSetFromQuery(query,ResultSet.TYPE_SCROLL_INSENSITIVE); 
  15.  
  16.          String tableAlias = reportClientDoc.getDatabaseController().getDatabase().getTables().getTable(0).getAlias(); 
  17.         //把结果集放进报表里,将会自动产生一个datasource 
  18.          reportClientDoc.getDatabaseController().setDataSource(resultSet,tableAlias, "resultsetTable"); 
  19.          session.setAttribute(session_name, reportClientDoc.getReportSource()); 
  20.          flag=true
  21.         return flag; 
  22.      } catch (Exception e) ...{ 
  23.         // TODO: handle exception 
  24.          e.printStackTrace(); 
  25.         return flag; 
  26.      }         
    /** *//**
      * 通过sql语句过滤报表数据,在.net就不用怎么惨了
     */
    public boolean isReportSourceInSession(String session_name,HttpSession session) throws ReportSDKException, SQLException, ClassNotFoundException...{
        boolean flag=false;
        try ...{
            //打开水晶报表
             ReportClientDocument reportClientDoc = new ReportClientDocument();
             reportClientDoc.open(REPORT_NAME, 0);
           <span style="color:#ff00;"> //sql查询语句,返回的字段数必须跟报表里面的一样,不能多也不能少,并且字段的类型要跟报表的一样,其他不管是什么数据都可以  
              //from 表这里要填完整,如数据库名.dbo.数据库表,最好做个别名
</span>             String query = "select tt.test_1,tt.test_2,tt.test_3,tt.test_4 from dname.dbo.test tt";
            
             ResultSet resultSet = this.getResultSetFromQuery(query,ResultSet.TYPE_SCROLL_INSENSITIVE);
    
             String tableAlias = reportClientDoc.getDatabaseController().getDatabase().getTables().getTable(0).getAlias();
            //把结果集放进报表里,将会自动产生一个datasource
             reportClientDoc.getDatabaseController().setDataSource(resultSet,tableAlias, "resultsetTable");
             session.setAttribute(session_name, reportClientDoc.getReportSource());
             flag=true;
            return flag;
         } catch (Exception e) ...{
            // TODO: handle exception
             e.printStackTrace();
            return flag;
         }        

最后动态显示的jsp代码为:

  1. <%@page import="com.JRC.util.JRC_ResultSet_DataSource" %> 
  2. <%--webreporting.jar  --%> 
  3. <%@page import="com.crystaldecisions.report.web.viewer.*" %> 
  4. <%--jrcerom.jar --%> 
  5. <%@ page import="com.crystaldecisions.reports.sdk.*" %> 
  6. <%     
  7.      JRC_ResultSet_DataSource jrcd=new JRC_ResultSet_DataSource("resultSet.rpt"); 
  8.     if(!jrcd.isReportSourceInSession("reportSource",session) 
  9.          response.sendRedirect("error.html"); 
  10.      CrystalReportViewer crViewer=new CrystalReportViewer(); 
  11.      crViewer.setOwnPage(true); 
  12.      crViewer.setOwnForm(true); 
  13.      crViewer.setPrintMode(CrPrintMode.ACTIVEX); 
  14.      
  15.      Object reportSource=session.getAttribute("reportSource"); 
  16.      crViewer.setReportSource(reportSource); 
  17.      
  18.      crViewer.processHttpRequest(request,response,this.getServletConfig().getServletContext(),null); 
  19. %> 
<%@page import="com.JRC.util.JRC_ResultSet_DataSource" %>
<%--webreporting.jar  --%>
<%@page import="com.crystaldecisions.report.web.viewer.*" %>
<%--jrcerom.jar --%>
<%@ page import="com.crystaldecisions.reports.sdk.*" %>
<%    
     JRC_ResultSet_DataSource jrcd=new JRC_ResultSet_DataSource("resultSet.rpt");
    if(!jrcd.isReportSourceInSession("reportSource",session)
         response.sendRedirect("error.html");
     CrystalReportViewer crViewer=new CrystalReportViewer();
     crViewer.setOwnPage(true);
     crViewer.setOwnForm(true);
     crViewer.setPrintMode(CrPrintMode.ACTIVEX);
    
     Object reportSource=session.getAttribute("reportSource");
     crViewer.setReportSource(reportSource);
    
     crViewer.processHttpRequest(request,response,this.getServletConfig().getServletContext(),null);
%>


这里值得我们注意的是关于数据源和查询的数据的字段。

第一:数据源必须和制作报表的时候用的数据源是一样的,也就是报表用的是sql2005,java连接的也是sql2005,

若用的是oracle,java连接的也是oracle。关于查询的sql语句中使用的字段也必须和制作报表用到的字段一模一样。

在制作报表的时候我们可能会用到某种表的某3个字段,在后台的sql语句我们也必须使用这个3个字段。

上述只是涉及到单表多表可能会比较麻烦一点。

本人先介绍到这里,至于多表综合显示看各位的需求了,若大家都需要我帮助各位解答的话,那各位留言吧。若有说的不够好的地方希望大家多多指正,谢谢。

资源文件:http://download.csdn.net/detail/ylovep/5213794

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值