VC2015调用Grid++report报表控件

发布时间:2020-06-15 05:54:05 作者:Chinayu2014
来源:网络 阅读:1352

Grid++report这是一个国产报表控件,从2.x就开始接触,基本所有的学习资源,来自于自带的文档和例子。能学多少靠摸索。整体功能还是不错的。他提供了3个控件,一个组件。在VC2015如果不想用控件,只想调用组件实现打印的功能,怎么办呢?步骤如下:

(1)在afxstd.h文件中包含头文件如下:

#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>

(2)在程序APP文件中,声明如下:

CComModule _Module;//全局变量
BOOL CMFCApplication1App::InitInstance()
{//初始化报表COM组件
    HRESULT hRes = ::CoInitialize(NULL);
    ATLASSERT(SUCCEEDED(hRes));
    _Module.Init(0, AfxGetInstanceHandle());
    //......
}
int CMFCApplication1App::ExitInstance()
{//重写虚函数
    _Module.Term();
    ::CoUninitialize();
    return CWinApp::ExitInstance();
}

(3)在对话框的头文件中,加入如下:

#include "GetPath.h"
#include "GRImport.h"

(4)当然需要先将Grid++report目录下的Utility文件夹复制到工程目录中,并在“项目属性->VC++目录->包含目录”添加Utility文件夹。

(5)在对话框窗口类中添加成员变量:IGridppReportPtr   m_pGridppReport;

//.h文件中添加
private:
   IGridppReportPtr   m_pGridppReport;
//.cpp文件中
BOOL CMFCApplication1Dlg::OnInitDialog()
{
     //创建报表主对象
     m_pGridppReport.CreateInstance(__uuidof(GridppReport));
     ATLASSERT(m_pGridppReport != NULL);
     //加载模板文件
     //从文件中载入报表模板数据到报表主对象
     CString FileName = GetReportTemplatePath(_T("标准过磅单1.grf"));
     m_pGridppReport->LoadFromFile((LPCTSTR)FileName);
     //第(7)步还有代码
}
void CMFCApplication1Dlg::OnDestroy()
{
     CDialogEx::OnDestroy();
     //释放主报表对象
     m_pGridppReport.Release();
}
void CMFCApplication1Dlg::OnBnClickedButton1()
{
     //直接打印报表
     m_pGridppReport->Print(TRUE);
}
void CMFCApplication1Dlg::OnBnClickedButton2()
{
     // 显示预览窗口
     m_pGridppReport->Title = _T("标准过磅单1");
     m_pGridppReport->PrintPreview(TRUE);
}

(6)同时将导出库复制到工程路径下,即gregn.tlb 和 grdes.tlb

(7)要动态修改报表参数,可以新建一个类,继承报表事件处理接口。

//Scale3DCReportEvent.h头文件
#pragma once
#include "GridppReportEventImpl.h"

class CScale3DCReportEvent :public CGridppReportEventImpl
{
public:
	CScale3DCReportEvent();
	~CScale3DCReportEvent();
	
        //初始化时自动调用
	virtual void Initialize(void);
	//填充数据时自动调用
	virtual void FetchRecord(void);
	//需要传入主报表对象
	IGridppReportPtr m_pGridppReport;
	//如果要推数据进报表,需要定义字段接口
	IGRFieldPtr   m_SerialNo;
};
//Scale3DCReportEvent.cpp文件
#include "stdafx.h"
#include "Scale3DCReportEvent.h"
#include "GridppReportEventImpl.c"

CScale3DCReportEvent::CScale3DCReportEvent()
{
}
CScale3DCReportEvent::~CScale3DCReportEvent()
{
}
void CScale3DCReportEvent::Initialize(void)
{
    //报表中的参数赋值
    m_pGridppReport->ParameterByName(_T("车号"))->AsString =_T("猎豹太空梭0X29");
    //获取字段接口
    m_SerialNo  = m_pReportView->m_pGridppReport->FieldByName("磅单流水号");
}
void CScale3DCStandardReportEvent::FetchRecord(void)
{//为字段接口赋值
	for (INT i = 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;//报表事件代理指针
IGridppReportPtr   m_pGridppReport;//报表主对象

在窗口的OnInitDialog()中添加如下代码:

//创建事件响应对象
CComObject<CScale3DCReportEvent> *pEvent;
CComObject<CScale3DCReportEvent>::CreateInstance(&pEvent);
m_pReportEvents = pEvent;
m_pReportEvents->AddRef();
pEvent->m_pGridppReport = m_pGridppReport;//事件代理指针
HRESULT hr = m_pReportEvents->DispEventAdvise(m_pGridppReport, 
             &__uuidof(_IGridppReportEvents));
ATLASSERT( SUCCEEDED(hr) );

(9)在调用中添加ON_WM_DESTROY消息,第5步中添加过,所以继续添加代码:

//释放事件代理
if (m_pReportEvents != NULL)
{
    HRESULT hr = 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();
CString FileName = 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"

//声明成员变量:
//关联控件
CGrdesigner1 m_DesignReport;
IGRDesignerPtr  m_pDesigner;//接口指针	
//传入一个主报表对象
IGridppReportPtr   m_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_MAP
	ON_EVENT(Scale3DCDesignReportDlg, IDC_GRDESIGNER1, 5, 
	DataChangeGrdesigner1, VTS_NONE)//报表内容已改变
END_EVENTSINK_MAP()

VOID CScale3DCBillDeisgnDlg::OnSaveReport()
{//响应报表设计控件上的按钮事件
	AfxMessageBox(_T("ok"));
	m_pDesigner->DefaultAction = FALSE;
}
void Scale3DCDesignReportDlg::DataChangeGrdesigner1()
{//报表内容已改变
	m_pDesigner->Dirty = TRUE;
}

在窗口的OnInitDialog函数中加入如下代码:

//报表设计容器指针
LPUNKNOWN spUnk = m_DesignReport.GetControlUnknown();
spUnk->QueryInterface(__uuidof(IGRDesigner), (void**)(&m_pDesigner));
ATLASSERT(m_pDesigner != NULL);
m_pDesigner->Report = m_pGridppReport;//编定报表主对象

(2)Grid++report报表中使用js脚本

var  price = Sender.DisplayText
if (price == "")
Sender.DisplayText = "0元"
else
Sender.DisplayText = price+"元"


推荐阅读:
  1. 用ActiveReports 报表控件,轻松搭建HIS医院系
  2. 在VC2015使用jsoncpp库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

grid++ report报表组件 gr

上一篇:php语法基础2

下一篇:微擎二次开发教程之系统目录结构

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》