方法一:
(也可以不用namespace,看个人习惯)
#include "craxddrt.h" //VC6中引入类型库中的类,并在生成的文件中添加名为CrystalReportWrapper的namespace
craxddrt.h修改:
namespace CrystalReportWrapper
{
...//craxddrt.h内容
}
craxddrt.cpp中在#include "craxddrt.h"后添加:
using namespace CrystalReportWrapper;
CCrystalReport10Viewer crReportViewer;
CrystalReportWrapper::IApplication crApplication;
CrystalReportWrapper::IReport crReport;
CrystalReportWrapper::IDatabase crDatabase;
CADODatabase adoDatabase;
CADOCommand adoCommand;
crApplication.CreateDispatch("CrystalDesignRunTime.Application.10");
COleVariant var((short)1);
crReport=crApplication.OpenReport("Report.rpt",var);
crDatabase=crReport.GetDatabase();
adoDatabase.ConnectAccess("soft.mdb");
adoCommand.SetDatabase(&adoDatabase);
adoCommand.SetText("SoftClass");
adoCommand.SetType(adCmdTable);
VARIANT var3;
var3.vt=VT_DISPATCH;
var3.punkVal=adoDatabase.m_pConnection;
VARIANT var4;
var4.vt=VT_DISPATCH;
var4.punkVal=adoCommand.GetCommand();
crDatabase.AddADOCommand(var3,var4);
crReportViewer.Create(NULL,WS_CHILD | WS_VISIBLE, CRect(0,0,0,0),this,1);
crReportViewer.put_ReportSource(crReport);
crReportViewer.Refresh();
crReportViewer.ViewReport();
方法二:
(注释为不带namespace)
#import "C:/Program Files/Common Files/Business Objects/2.7/Bin/craxddrt.dll"
//#import "C:/Program Files/Common Files/Business Objects/2.7/Bin/craxddrt.dll" no_namespace
CCrystalReport10Viewer crReportViewer;
CRAXDDRT::IApplicationPtr crApplication;
CRAXDDRT::IReportPtr crReport;
CRAXDDRT::IDatabasePtr crDatabase;
//IApplicationPtr crApplication;
//IReportPtr crReport;
//IDatabasePtr crDatabase;
CADODatabase adoDatabase;
CADOCommand adoCommand;
HRESULT hr = crApplication.CreateInstance(__uuidof(CRAXDDRT::Application));
//HRESULT hr = crApplication.CreateInstance(__uuidof(Application));
if(FAILED(hr))
{
MessageBox(_T("水晶报表初始化出错!"));
return -1;
}
COleVariant var((short)1);
crReport=crApplication->OpenReport("Report.rpt",var);
crDatabase=crReport->GetDatabase();
adoDatabase.ConnectAccess("soft.mdb");
adoCommand.SetDatabase(&adoDatabase);
adoCommand.SetText("SoftClass");
adoCommand.SetType(adCmdTable);
VARIANT var3;
var3.vt=VT_DISPATCH;
var3.punkVal=adoDatabase.m_pConnection;
VARIANT var4;
var4.vt=VT_DISPATCH;
var4.punkVal=adoCommand.GetCommand();
crDatabase->AddADOCommand(var3,var4);
crReportViewer.Create(NULL,WS_CHILD | WS_VISIBLE, CRect(0,0,0,0),this,1);
crReportViewer.put_ReportSource(crReport);
crReportViewer.Refresh();
crReportViewer.ViewReport();
方法三:
//这几个头文件由VC7.1添加类型库中的类产生,添加所需要的类
#include "CrystalReport10Application.h"
#include "CrystalReport10Report.h"
#include "CrystalReport10Database.h"
#include "CrystalReport10Sections.h"
#include "CrystalReport10Designer.h"
CCrystalReport10Viewer crReportViewer;
CCrystalReport10Application crApplication;
CCrystalReport10Report crReport;
CCrystalReport10Database crDatabase;
CADOCommand adoCommand;
crApplication.CreateDispatch("CrystalDesignRunTime.Application.10");
COleVariant var((short)1);
crReport=crApplication.OpenReport("Report.rpt",var);
crReport.put_EnableAsyncQuery(TRUE);
crDatabase=crReport.get_Database();
adoCommand.SetDatabase(&theApp.m_pConnection);
adoCommand.SetText("SoftClass");
adoCommand.SetType(adCmdTable);
VARIANT var3;
var3.vt=VT_DISPATCH;
var3.punkVal=theApp.m_pConnection.m_pConnection;
VARIANT var4;
var4.vt=VT_DISPATCH;
var4.punkVal=adoCommand.GetCommand();
//crDatabase.AddADOCommand(var3,var4);
crReportViewer.Create(NULL,WS_CHILD | WS_VISIBLE, CRect(0,0,0,0),this,1);
crReportViewer.put_ReportSource(crReport);
crReportViewer.ViewReport();