VC2015调用Grid++report报表控件
Grid++report这是一个国产报表控件,从2.x就开始接触,基本所有的学习资源,来自于自带的文档和例子。能学多少靠摸索。整体功能还是不错的。他提供了3个控件,一个组件。在VC2015如果不想用控件,只想调用组件实现打印的功能,怎么办呢?步骤如下:
(1)在afxstd.h文件中包含头文件如下:
#include<atlbase.h>externCComModule_Module;#include<atlcom.h>
(2)在程序APP文件中,声明如下:
CComModule_Module;//全局变量BOOLCMFCApplication1App::InitInstance(){//初始化报表COM组件HRESULThRes=::CoInitialize(NULL);ATLASSERT(SUCCEEDED(hRes));_Module.Init(0,AfxGetInstanceHandle());//......}intCMFCApplication1App::ExitInstance(){//重写虚函数_Module.Term();::CoUninitialize();returnCWinApp::ExitInstance();}
(3)在对话框的头文件中,加入如下:
#include"GetPath.h"#include"GRImport.h"
(4)当然需要先将Grid++report目录下的Utility文件夹复制到工程目录中,并在“项目属性->VC++目录->包含目录”添加Utility文件夹。
(5)在对话框窗口类中添加成员变量:IGridppReportPtr m_pGridppReport;
//.h文件中添加private:IGridppReportPtrm_pGridppReport;//.cpp文件中BOOLCMFCApplication1Dlg::OnInitDialog(){//创建报表主对象m_pGridppReport.CreateInstance(__uuidof(GridppReport));ATLASSERT(m_pGridppReport!=NULL);//加载模板文件//从文件中载入报表模板数据到报表主对象CStringFileName=GetReportTemplatePath(_T("标准过磅单1.grf"));m_pGridppReport->LoadFromFile((LPCTSTR)FileName);//第(7)步还有代码}
voidCMFCApplication1Dlg::OnDestroy(){CDialogEx::OnDestroy();//释放主报表对象m_pGridppReport.Release();}voidCMFCApplication1Dlg::OnBnClickedButton1(){//直接打印报表m_pGridppReport->Print(TRUE);}voidCMFCApplication1Dlg::OnBnClickedButton2(){//显示预览窗口m_pGridppReport->Title=_T("标准过磅单1");m_pGridppReport->PrintPreview(TRUE);}
(6)同时将导出库复制到工程路径下,即gregn.tlb 和 grdes.tlb
(7)要动态修改报表参数,可以新建一个类,继承报表事件处理接口。
//Scale3DCReportEvent.h头文件#pragmaonce#include"GridppReportEventImpl.h"classCScale3DCReportEvent:publicCGridppReportEventImpl{public:CScale3DCReportEvent();~CScale3DCReportEvent();//初始化时自动调用virtualvoidInitialize(void);//填充数据时自动调用virtualvoidFetchRecord(void);//需要传入主报表对象IGridppReportPtrm_pGridppReport;//如果要推数据进报表,需要定义字段接口IGRFieldPtrm_SerialNo;};
//Scale3DCReportEvent.cpp文件#include"stdafx.h"#include"Scale3DCReportEvent.h"#include"GridppReportEventImpl.c"CScale3DCReportEvent::CScale3DCReportEvent(){}CScale3DCReportEvent::~CScale3DCReportEvent(){}voidCScale3DCReportEvent::Initialize(void){//报表中的参数赋值m_pGridppReport->ParameterByName(_T("车号"))->AsString=_T("猎豹太空梭0X29");//获取字段接口m_SerialNo=m_pReportView->m_pGridppReport->FieldByName("磅单流水号");}voidCScale3DCStandardReportEvent::FetchRecord(void){//为字段接口赋值for(INTi=0;i<m_db.m_data.size();i++){m_pReportView->m_pGridppReport->DetailGrid->Recordset->Append();FillRecord1(i);m_pReportView->m_pGridppReport->DetailGrid->Recordset->Post();}}
(8)在相应的对话框头文件中包含:
#include "Scale3DCReportEvent.h"//事件包装类
#include "GetPath.h" //上面步骤已包含过
#include "GRImport.h"
在窗口类中声明成员变量:
//.h文件中private:CGridppReportEventImpl*m_pReportEvents;//报表事件代理指针IGridppReportPtrm_pGridppReport;//报表主对象
在窗口的OnInitDialog()中添加如下代码:
//创建事件响应对象CComObject<CScale3DCReportEvent>*pEvent;CComObject<CScale3DCReportEvent>::CreateInstance(&pEvent);m_pReportEvents=pEvent;m_pReportEvents->AddRef();pEvent->m_pGridppReport=m_pGridppReport;//事件代理指针HRESULThr=m_pReportEvents->DispEventAdvise(m_pGridppReport,&__uuidof(_IGridppReportEvents));ATLASSERT(SUCCEEDED(hr));
(9)在调用中添加ON_WM_DESTROY消息,第5步中添加过,所以继续添加代码:
//释放事件代理if(m_pReportEvents!=NULL){HRESULThr=m_pReportEvents->DispEventUnadvise(m_pGridppReport,&__uuidof(_IGridppReportEvents));m_pReportEvents->Release();m_pReportEvents=NULL;ATLASSERT(SUCCEEDED(hr));}//释放主报表对象m_pGridppReport.Release();//释放接口指针m_pPrintViewer.Release();
(10)报表显示控件的使用
m_printView.Stop();CStringFileName=GetReportTemplatePath(strFileName);m_pGridppReport->LoadFromFile((LPCTSTR)FileName);m_printView.put_Report(m_pGridppReport);m_printView.Refresh();m_printView.Start();m_printView.ZoomToHeight();
其他功能:
(1)报表设计器控件使用
在窗口上放上报表设计器控件,在窗口头文件中加入:
#include"GetPath.h"#include"GRImport.h"#include"GridppReportEventImpl.h"//声明成员变量://关联控件CGrdesigner1m_DesignReport;IGRDesignerPtrm_pDesigner;//接口指针//传入一个主报表对象IGridppReportPtrm_pGridppReport;//添加宏声明DECLARE_EVENTSINK_MAP()
在窗口的.CPP文件中加入如下内容:
#include"GridppReportEventImpl.c"BEGIN_EVENTSINK_MAP(CScale3DCBillDeisgnDlg,CDialogEx)//{{AFX_EVENTSINK_MAP(CScale3DCBillDeisgnDlg)ON_EVENT(CScale3DCBillDeisgnDlg,IDC_GRDESIGNER1,7/*SaveReport*/,OnSaveReport,VTS_NONE)//}}AFX_EVENTSINK_MAPON_EVENT(Scale3DCDesignReportDlg,IDC_GRDESIGNER1,5,DataChangeGrdesigner1,VTS_NONE)//报表内容已改变END_EVENTSINK_MAP()VOIDCScale3DCBillDeisgnDlg::OnSaveReport(){//响应报表设计控件上的按钮事件AfxMessageBox(_T("ok"));m_pDesigner->DefaultAction=FALSE;}voidScale3DCDesignReportDlg::DataChangeGrdesigner1(){//报表内容已改变m_pDesigner->Dirty=TRUE;}
在窗口的OnInitDialog函数中加入如下代码:
//报表设计容器指针LPUNKNOWNspUnk=m_DesignReport.GetControlUnknown();spUnk->QueryInterface(__uuidof(IGRDesigner),(void**)(&m_pDesigner));ATLASSERT(m_pDesigner!=NULL);m_pDesigner->Report=m_pGridppReport;//编定报表主对象
(2)Grid++report报表中使用js脚本
varprice=Sender.DisplayTextif(price=="")Sender.DisplayText="0元"elseSender.DisplayText=price+"元"
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。