在.net中,你可以很容易的用sql語句過濾報(bào)表數(shù)據(jù),但在CR4E中沒這樣的功能設(shè)定,但可以通過編寫代碼完成。
這里是我做的簡單用sql語句過濾數(shù)據(jù)的例子。
項(xiàng)目還是用ǘ ├錈嫻膖omcat項(xiàng)目。
建一個(gè)實(shí)現(xiàn)這個(gè)功能的類
JRC_ResultSet_DataSource.java
package com.JRC.util;
import java.sql.*;
import javax.servlet.http.*;

import com.crystaldecisions.reports.sdk.*;
import com.crystaldecisions.sdk.occa.report.lib.*;


public class JRC_ResultSet_DataSource ...{
private String REPORT_NAME="";

public JRC_ResultSet_DataSource(String report_name)...{
this.REPORT_NAME=report_name;
}

/** *//**
* @return rEPORT_NAME
*/

public String getREPORT_NAME() ...{
return REPORT_NAME;
}


/** *//**
* @param report_name 要設(shè)置的 rEPORT_NAME
*/

public void setREPORT_NAME(String report_name) ...{
REPORT_NAME = report_name;
}


/** *//**
* 連接數(shù)據(jù)庫,通過sql查詢語句進(jìn)行查詢,返回結(jié)果集
*/
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);

}

/** *//**
* 通過sql語句過濾報(bào)表數(shù)據(jù),在.net就不用怎么慘了
*/

public boolean isReportSourceInSession(String session_name,HttpSession session) throws ReportSDKException, SQLException, ClassNotFoundException...{
boolean flag=false;

try ...{
//打開水晶報(bào)表
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open(REPORT_NAME, 0);
//sql查詢語句,返回的字段數(shù)必須跟報(bào)表里面的一樣,不能多也不能少,并且字段的類型要跟報(bào)表的一樣,其他不管是什么數(shù)據(jù)都可以
//from 表這里要填完整,如數(shù)據(jù)庫名.dbo.數(shù)據(jù)庫表,最好做個(gè)別名
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();
//把結(jié)果集放進(jìn)報(bào)表里,將會(huì)自動(dòng)產(chǎn)生一個(gè)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;
}
}
}

這里要注意數(shù)據(jù)庫查詢結(jié)果集的字段數(shù)目要跟報(bào)表里面的字段數(shù)目一樣,類型也要一樣,不然就會(huì)出錯(cuò)。
sql語句中的表的名字要完整,如數(shù)據(jù)庫名.dbo.數(shù)據(jù)庫表,最好做個(gè)別名
顯示頁面
Result_viewer.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);
%>
注意上面幾點(diǎn)應(yīng)該就沒問題了
PS:Snippets視圖最下面有Crystal reports的一些實(shí)用代碼段
CR查看器標(biāo)記、打開并查詢報(bào)表、打開報(bào)表、查看報(bào)表、查看報(bào)表并設(shè)置數(shù)據(jù)庫登錄、將報(bào)表導(dǎo)出為pdf、將報(bào)表導(dǎo)出為rtf 等的代碼段,簡單易用
posted on 2008-03-28 16:48
煒 閱讀(537)
評(píng)論(0) 編輯 收藏 所屬分類:
java基礎(chǔ)