水晶報表是一個報表設計開發的強大工具,功能強大,設計靈活,在水晶報表光碟片中只提供了一個完全動態產生報表的例子,使用繁瑣。現介紹其在VC++6.0中的簡單使用方法。編譯環境:VC++6.0 sp5 、Windows 2000 Server sp3 (en)。一、匯入水晶報表使用的動態聯結庫:根據實際修改檔案路徑。#import "C:Documents and SettingsAdministrator案頭crcraxdrt9.dll" no_namespace
二、定義介面指標變數//水晶報表變數
IApplicationPtr m_Application;
IReportPtr m_Report;
//水晶報表控制項變數,在對話方塊中加入該控制項
CCrystalReportViewer9 m_CRViewer1;三、具體實現步驟 //執行個體化m_Application
m_Application.CreateInstance (__uuidof(Application));
//擷取m_Report變數
//staff.rpt為通過嚮導建立的報表檔案,資料庫採用SQL Server 7.0
m_Report =m_Application->OpenReport ("C:Documents and SettingsAdministrator案頭crdebugstaff.rpt");
//設定報表標題
m_Report->put_ReportTitle (_bstr_t("Title"));
//設定資料庫連接變數
//資料庫伺服器(local),資料庫名staff,使用者名稱sa,密碼sa
m_Report->Database ->Tables ->Item [1]->SetLogOnInfo("(local)","staff","sa","sa");
//設定檢索SQL命令
m_Report->put_SQLQueryString ((_bstr_t)"select * from person where id<'4' order by id");
//不顯示重複欄位
m_Report->PutEnableSelectDistinctRecords (TRUE);
//設定檢索條件,採用水晶報表文法,功能同設定檢索SQL命令
m_Report->PutRecordSelectionFormula ((_bstr_t)"{person.id}='1'");
//設定報表作者
m_Report->PutReportAuthor ("xiaojin");
//將m_Report與報表控制項串連
m_CRViewer1.SetReportSource(m_Report);
//顯示報表工具條
m_CRViewer1.SetDisplayToolbar (TRUE);
//不顯示報表左邊的分組樹
m_CRViewer1.SetDisplayGroupTree (FALSE);
//不顯示控制項邊框
m_CRViewer1.SetDisplayBorder (FALSE);
//重新整理資料
m_CRViewer1.Refresh ();
//顯示報表內容
m_CRViewer1.ViewReport();這樣,漂亮的水晶報表就顯示出來了。程式異常處理部分省略,請自行加入。