水晶报表在VC6.0下的使用指南

来源:互联网 发布:软件资源主要特点是 编辑:程序博客网 时间:2024/05/16 07:37

水晶报表在VC6.0下的使用指南

    水晶报表(Crystal Report)是业内最专业、功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流行开发工具的集成和接口。在VS.Net平台 做过报表开发的程序员,一定都对水晶报表强大、高效、集成等特性留下了深刻印象。 除了开发新程序外,在工作中我们常需要接触到很多较早的软件系统报表功能升级的需求,如果能结合水晶报表这一强大的工具,往往能事半功倍。

       但是水晶报表在VC6.0中开发的文档不是很齐全,给开发带来了相当多的不便于困难。

 

水晶报表运行环境的搭建

Crystal Reports Developer’s Help 中关于在VC中调用水晶报表的例子如下

Printing a Report through Visual C++

To print a report through Visual C++
  1. Open Visual C++ 6.0 if it isn't already running.
  1. On the File menu, select New.
  1. In the New dialog box, select the MFC AppWizard (exe) from the Projects tab.
  1. Type MyRDC for the Project Name and click OK.
  1. In the first step of the wizard, select Dialog based and click Finish to accept the defaults for the MFC AppWizard, and then click OK in the New Project Information dialog box.

When the wizard executes, a dialog box appears in design mode.

  1. Right-click the dialog box and select Insert ActiveX Control from the popup dialog box. From the list of available controls, select Crystal Report Viewer Control.
  1. Resize the control to the desired size.
  1. Rearrange the OK and Cancel buttons to their desired location.
  1. Select the View|Class Wizard.
  1. Click the Member Variables tab and ensure that the class name is CMyRDCDlg.
  1. Add a variable for the IDC_CRVIEWER1 and click OK when prompted to add the control to the project.
  1. Name the member variable for the viewer m_Viewer.
  1. Click OK to save the changes and then close the dialog box.
  1. Add a reference to the RDC runtime object Model.
  1. On the File|Open menu, select the MyRDCdlg.h file that was generated by the MFC App wizard and click Open.
  1. On the File|Open menu, open the MyRDC.CPP file and add the following code before the message map:

struct InitOle {

InitOle()  { ::CoInitialize(NULL); }

~InitOle() { ::CoUninitialize();   }

} _init_InitOle_;

  1. Declare protected member variables for the RDC's Application and Report objects in the implementation section of the MyRDCdlg.h header file.

For optional Variant parameters declare a protected dummy variable in the same section.

IApplicationPtr m_Application;

IReportPtr m_Report;

VARIANT dummy;

  1. Add the following constants after the #endif statement in the declarations section of the MyRDCdlg.CPP file.

// The constants needed to create the Application and Report Objects COM objects

const CLSID CLSID_Application = {0xb4741fd0,0x45a6,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};

const IID IID_IApplication = {0x0bac5cf2,0x44c9,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};

const CLSID CLSID_ReportObjects = {0xb4741e60,0x45a6,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};

const IID IID_IReportObjects = {0x0bac59b2,0x44c9,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};

  1. Also in the MyRDCdlg.CPP file, add the following code to the CMyRDCDlg::OnInitDialog() method just before the return statement:

// A dummy variant

VariantInit (&dummy);

dummy.vt = VT_EMPTY;

HRESULT            hr = S_OK;

IApplicationPtr m_Application = NULL;

IReportPtr m_Report = NULL;

            

// Specify the path to the report you want to print

_bstr_t ReportPath("c://Program Files//Crystal Decisions//Crystal Reports 9//Samples//En//Reports//General Business//Inventory.rpt");

_variant_t vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);

 

// Instantiate the IApplication object

hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER , IID_IApplication, (void **) &m_Application);

 

//Open the Report using the OpenReport method

m_Report = m_Application->OpenReport(ReportPath, dummy);

 

//Print the Report to window

m_Viewer.SetReportSource(m_Report);

m_Viewer.ViewReport();

  1. Finally, on the Build menu, select Rebuild All, then Execute.

When the app runs, it opens the report and displays it in the viewer control on the application's dialog. Click OK or Cancel to dismiss the dialog and terminate the application.

Opening and Printing a Crystal Report through the RDC

To open and print a Crystal Report through the RDC
  1. On the File|Open menu, open the MyRDC.CPP file. Add the following code to the MyRDC.InitInstance() method just before the return statement:

// A dummy variant

VariantInit (&dummy);

dummy.vt = VT_EMPTY;

HRESULT       hr = S_OK;

              IApplicationPtr m_Application = NULL;

              IReportPtr m_Report = NULL;

 

// Specify the path to the report you want to print                                    

_bstr_t ReportPath("c://Program Files//Crystal Decisions//Crystal Reports 9 //Samples//En//Reports//General Business//Inventory.rpt");

_variant_t vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);

// Instantiate the IApplication object

hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER , IID_IApplication, (void **) & CRAXDRT::IApplication);

//Open the Report using the OpenReport method

m_Report = m_Application->OpenReport(ReportPath, dummy)

//Print the Report to printer

m_Report->PrintOut(dummy, dummy, dummy, dummy

);

  1. Finally, on the Build menu, select Rebuild All.

 

在这段例子中虽然有构建基本程序的必要元素,但是缺乏动态绑定数据源的能力,给实际使用带来了相当的不便。

 

水晶报表模式的选择

   水晶报表包含两种模式,一种是Pull模式,一种是Push模式.下面句先具体的谈谈两种模式的区别.
       Pull
模式是一种直接把数据从数据库里面拉出来的模式,所以很形象的叫做Pull,Pull模式主要是要跟数据库建立连接,从数据库里面 把数据拉出来并且显示在报表上面.Pull模式的好处就是根本不用你写一行代码,就可以完成报表数据的读取和报表的制作
.
        Push
模式相对于pull模式来说是一种比较高级的模式,Push模式所要得到的数据是需要用到ADO.NET来获取数据,然后在报表上面显示出 .Push模式可以实现数据的自定义获取,这样可以实现功能自定义的最大化,但是相对来说需要编写一定程度的代码(即获取数据
).
        Pull
模式和Push模式总的说来感觉只是获取数据的方式不同,不管用那种方式,数据获取完成之后只需要把获取的数据和制作的水晶报表联系起来就OK
.
        
下面就具体的两种模式获取数据做相关的说明
:
        
.pull模式

            
先要从工具箱的水晶报表工具栏里面分别拖出CrystalReportSource控件和 CrystalReportViewer控件.CrystalReportSource控件和其他的数据源控件的功能类似,就是与已经制作好的相关的报表 建立连接.CrystalReportViewer控件是用来在WEB上面显示水晶报表的,它的主要作用是跟已经与水晶报表建立了联系的 CrystalReportSource建立联系,以方便让用户在WEB上面能够查看水晶报表,所以CrystalReportViewer又叫做水晶报 表查看器,两者缺一都是在Pull模式下不能正常显示水晶报表的.CrystalReportSource需要指定一个水晶报表文件,如果还没有水晶报表 的化,可以通过CrystalReportSource的向导创建一个新的水晶报表,在创建的过程中指定为空白报表,报表创建好之后就会在左侧出现一个字 段资源管理器,右键点击数据库字段并且选择数据库专家就会弹出一个数据库专家的对话框,在创建新连接里面选择OLE DB(ADO)选项,因为我的是SQL SERVER 2005 数据库,所以我在弹出的数据源配置里面选中SQL Native Client,点击下一步,依次的输入服务器名,用户名,密码,数据库,然后点确定就发现在左侧的数据库字段里面出现了数据库里面包含的所有字段,这样一 Pull模式的数据连接就算成功了,然后就是根据我们需要的水晶报表的样式创建具体的水晶报表(具体怎么做下此再写一篇),在这里提示一下,如果是要显 示数据库里面的信息的话,只需要把字段拖放到水晶报表里面就可以了.最后,CrystalReportViewer的数据源指定为配置了的 CrystalReportSource.基本上一个Pull模式水晶报表的建立就完成了.
       
.push模式

        Push
模式主要显示自定义的数据,首先需要在项目里面建立一个DataSet,具体的方法是在项目里面添加一个数据集,具体方法是右键 选中解决资源管理器里面的项目的根目录,选中添加新项,选择数据集并且命名为MyDataSet.XSD,创建好数据集后有两种方式填充数据,第一就是可 以直接从服务器资源管理器里面拖动相关的数据字段到数据集合里面,第二就是自己创建数据集里面的字段,在数据填充的时候通过ADO.NET来实现数据的填 ,不过填充的DataSet是我们自己定义的MyDataSet,其实在我们填充的MyDataSet里面可以有很多张我们自己根据需要定义的表和字 ,这样可以实现我们的数据自定义最大化,数据填充以后就是关于水晶报表数据源的配置问题,Push模式的方式下,我们右键点击水晶报表字段资源管理器 里面的数据库字段,并且选中项目数据下面的ADO.NET数据集.然后我们可以清楚的看到在ADO.NET数据集下面有我们自定义的MyDataSet 据集,选中里面我们自己定义的数据集下面自己定义的表并且把表加入到右侧的栏目中,这样我们就可以像Pull模式一样来使用相关的字段建立起水晶报表了。

       从介绍我们可以看到Push模式具有很强的自定义能力,并且可以在数据库模式的改变与数据库的的更换的情况下不改变报表。由于VC6.0使用的是ADO,使用Push模式需要组装RecordSet,但是笔者没有找到这方面的资料而网上,大部分关于push模式的资料都是关于ADO.NET因而本文主要针对在PULL模式下动态绑定数据库。

动态选择数据库

       由于报表是在开发时设计的,无法预知实际使用中所连接的数据库位置信息,帐户信息等,因而动态选择数据库的第一步是动态设置数据源。

       相关代码如下:

       m_Report->Database->Tables->Item[1]->SetLogOnInfo

(“IP”,”数据库名,“账号”,“密码”)

Ip代表目标数据源的IP地址.

数据库名代表访问的数据库名。

账号代表访问数据库的帐户。

密码代表访问数据库的密码。

 

       在选择合适的数据源后就可以将设计时的文档与运行时文档分开。但是实际中可能还需要使用查询条件,这一点可以使用设置参数来实现。实现代码为

m_Report->ParameterFields->GetItemByName("id")->SetCurrentValue(postdata);

“ID ”代表报表中参数的名字。

 

       至此水晶报表中的基本环境已经配置完成,对于一般应用已经足矣。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 农村在城里上学怎么办? 父母没钱不工作怎么办 qq被家长偷看怎么办 孩子考差了怎么办 父母翻看qq记录怎么办 孩子qq屏蔽父母怎么办 杜鹃花老叶脱落怎么办 父母对孩子不好怎么办 家长看孩子手机怎么办 六年级打四年级学生怎么办 儿童被家长打怎么办 老师打孩子耳光怎么办 孩子动手打母亲怎么办 小孩被老师体罚怎么办 儿子跟父母对打怎么办 两个孩子争东西怎么办 大人吵架小孩该怎么办 小孩老爱顶嘴怎么办 孩子老爱顶嘴怎么办 孩子不愿分房睡怎么办 孩子不愿意去补课怎么办 高中孩子不愿意补课怎么办 孩子协调性不好怎么办 孩子暑假不愿意补课怎么办 话唠的孩子怎么办 孩孑不做作业怎么办 孩子有消极情绪怎么办 孩子太依赖奶奶怎么办 缺少母爱的孩子怎么办 孩子分房睡害怕怎么办 孩子厌烦写作业怎么办 孩子怕黑胆小怎么办 孩子性格太急躁怎么办 性格急躁的孩子怎么办 我儿子脾气暴躁怎么办 我妈脾气不好怎么办 妈妈更年期怎么办 脾气暴躁 宝妈脾气不好怎么办 我妈妈脾气不好怎么办 孩子写作业拖拖拉拉怎么办 家长指出老师错误怎么办