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>

具體<reportlocation>../../rpt_report</reportlocation>的含義自己可以百度,我在這裏說下我配置的意思是,水晶報表加載的報表文件也就是rpt文件的路徑在WebRoot中的rpt_report文件夾中若配置成<reportlocation>../..</reportlocation>就是說明加載的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文件夾中,也就是剛纔我們配置的路徑。

若報表能夠顯示那說明我們從頭到現在都沒有問題,其中報表自己製作。至於如何製作可以參考

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/樓主寫的。按照裏面的介紹然後自己寫的話應該沒有問題。

關鍵代碼是

第一獲取數據源大家應該沒有什麼問題。

/** *//**
      *    連接數據庫,通過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);
           <span style="color:#ff0000;"> //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代碼爲:

<%@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個字段。

 

上述只是涉及到單表多表可能會比較麻煩一點。

本人先介紹到這裏,至於多表綜合顯示看各位的需求了,若大家都需要我幫助各位解答的話,那各位留言吧。若有說的不夠好的地方希望大家多多指正,謝謝。


Demo實例下載地址:http://download.csdn.net/detail/lovin_fang/8175473





本文是轉載網上的一些學習文件,並非本人原創,如有侵權,請及時聯繫我刪除改正!謝謝。

我只是將學習過的東西放入自己的csdn中做個筆記

轉載源文件:http://blog.csdn.net/ylovep/article/details/8056716



發佈了12 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章