一、導入水晶報表使用的動態聯接庫:根據實際修改文件路徑。
#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 Settings\Administrator\桌面\cr\debug\staff.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();