Advertisement

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

阅读量:

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

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

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

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

复制代码
      <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配置如下:

复制代码
 <?xml version="1.0" encoding="utf-8"?>

    
 <CrystalReportEngine-configuration>
    
 	<reportlocation>../../rpt_report</reportlocation>
    
 	<timeout>10</timeout>
    
 	<keycode>AV864-01CS00G-0ZG518J</keycode>
    
 </CrystalReportEngine-configuration>

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

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

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

basic.jsp

复制代码
 <%@ 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文件夹中,也就是刚才我们配置的路径。

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

<> 应该容易看懂并且掌握简单的制作。

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

关键代码是

复制代码
    第一获取数据源大家应该没有什么问题。
复制代码
 /** *//** *    连接数据库,通过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);
    
  
    
      }
复制代码
    将查询出来的数据放到报表源中也就是reportClientDoc.getDatabaseController().setDataSource(resultSet,tableAlias, "resultsetTable");
复制代码
     /** *//** * 通过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);
    
         //sql查询语句,返回的字段数必须跟报表里面的一样,不能多也不能少,并且字段的类型要跟报表的一样,其他不管是什么数据都可以  
    
           //from 表这里要填完整,如数据库名.dbo.数据库表,最好做个别名
    
          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代码为:

复制代码
 <%@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个字段。

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

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

资源文件:<>

全部评论 (0)

还没有任何评论哟~