⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportctl.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ReportCtl.cpp : Implementation of the CReportCtrl ActiveX Control class.

#include "stdafx.h"
#include "report.h"
#include "ReportCtl.h"
#include "ReportPpg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CReportCtrl, COleControl)
const short CReportCtrl::ReportTypeNum=3;
const short CReportCtrl::ReportCharacterNum=3;
const CString CReportCtrl:: m_Errorinformation[]=
{"OK","DateSourceFileNoExist",
"FilePathNoExist","ReportTemplateFileNoExist",
"ReportFileNameIllegal","GroupPropertyNoexist",
"DataSourceKeyNoExist","ExcelCabNotStartOROtherExcelProbelm",
"DateSourceFileTypeError","ReportFileTypeError",
"ReportTemplateFileTypeError","WordCabNotStartOROtherWordProbelm"
};
/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CReportCtrl, COleControl)
	//{{AFX_MSG_MAP(CReportCtrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CReportCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CReportCtrl)
	DISP_PROPERTY_NOTIFY(CReportCtrl, "ReportTemplateFileName", m_reportTemplateFileName, OnReportTemplateFileNameChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CReportCtrl, "ReportFileName", m_reportFileName, OnReportFileNameChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CReportCtrl, "GroupProperty", m_groupProperty, OnGroupPropertyChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CReportCtrl, "FilePath", m_filePath, OnFilePathChanged, VT_BSTR)
	DISP_PROPERTY_NOTIFY(CReportCtrl, "DataSourceKey", m_dataSourceKey, OnDataSourceKeyChanged, VT_BSTR)
	DISP_PROPERTY_EX(CReportCtrl, "DateSourceName", GetDateSourceName, SetDateSourceName, VT_BSTR)
	DISP_PROPERTY_EX(CReportCtrl, "ReportType", GetReportType, SetReportType, VT_I2)
	DISP_PROPERTY_EX(CReportCtrl, "ReportCharacter", GetReportCharacter, SetReportCharacter, VT_I2)
	DISP_FUNCTION(CReportCtrl, "GetErrorInformation", GetErrorInformation, VT_BSTR, VTS_NONE)
	DISP_FUNCTION(CReportCtrl, "MakeReport", MakeReport, VT_I2, VTS_NONE)
	DISP_FUNCTION(CReportCtrl, "QuitExcel", QuitExcel, VT_EMPTY, VTS_NONE)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CReportCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CReportCtrl, COleControl)
	//{{AFX_EVENT_MAP(CReportCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CReportCtrl, 1)
	PROPPAGEID(CReportPropPage::guid)
END_PROPPAGEIDS(CReportCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CReportCtrl, "REPORT.ReportCtrl.1",
	0x3b7f977a, 0x7926, 0x4ec5, 0x97, 0x7e, 0x94, 0x5a, 0x3a, 0x19, 0xe8, 0x62)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CReportCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DReport =
		{ 0x2c0c8b70, 0x484b, 0x497d, { 0xb8, 0xfd, 0x12, 0xe2, 0x31, 0xaf, 0xf4, 0xe2 } };
const IID BASED_CODE IID_DReportEvents =
		{ 0x334267be, 0xe9e0, 0x42e0, { 0x85, 0x13, 0x7d, 0xc7, 0xfd, 0x3, 0xb0, 0xc2 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwReportOleMisc =
	OLEMISC_INVISIBLEATRUNTIME |
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CReportCtrl, IDS_REPORT, _dwReportOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::CReportCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CReportCtrl

BOOL CReportCtrl::CReportCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_REPORT,
			IDB_REPORT,
			afxRegApartmentThreading,
			_dwReportOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::CReportCtrl - Constructor

CReportCtrl::CReportCtrl()
{  char currentdir[100];
	InitializeIIDs(&IID_DReport, &IID_DReportEvents);
 m_ReportType=OneRecordOneReport;
	m_ReportCharacter= EveryReportPageAlone;
m_reportFileName.Insert (0,"TempReport.doc");
m_ErrorinformationCode=0;
GetCurrentDirectory(100,currentdir);
m_filePath.Insert (0,currentdir);
pGroupDataSourcePropertySN=NULL;
	  pDataSourcePropertyKeySN=NULL;
	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::~CReportCtrl - Destructor

CReportCtrl::~CReportCtrl()
{if(pGroupDataSourcePropertySN){delete pGroupDataSourcePropertySN;
pGroupDataSourcePropertySN=NULL;}
if(pDataSourcePropertyKeySN){delete pDataSourcePropertyKeySN;
pDataSourcePropertyKeySN=NULL;}
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::OnDraw - Drawing function

void CReportCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	pdc->Ellipse(rcBounds);
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::DoPropExchange - Persistence support

void CReportCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::OnResetState - Reset control to default state

void CReportCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl::AboutBox - Display an "About" box to the user

void CReportCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_REPORT);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CReportCtrl message handlers

void CReportCtrl::OnReportTemplateFileNameChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

BSTR CReportCtrl::GetDateSourceName() 
{
	 return  m_DateSourceName.AllocSysString();
}

void CReportCtrl::SetDateSourceName(LPCTSTR lpszNewValue) 
{
	// TODO: Add your property handler here
m_DateSourceName.Empty ();
m_DateSourceName.Insert (0,lpszNewValue);
SetModifiedFlag();

	SetModifiedFlag();
}

short CReportCtrl::GetReportType() 
{
	// TODO: Add your property handler here

return 	m_ReportType;
}

void CReportCtrl::SetReportType(short nNewValue) 
{
	// TODO: Add your property handler here
 if(nNewValue>ReportTypeNum ||nNewValue<1) nNewValue=1;
	m_ReportType=(ReportType)nNewValue;

	SetModifiedFlag();
}

void CReportCtrl::OnReportFileNameChanged() 
{
	// TODO: Add notification handler code
//m_reportFileName
	DeleteSpace(m_reportFileName,3);
	SetModifiedFlag();
}

short CReportCtrl::GetReportCharacter() 
{
	// TODO: Add your property handler here
return  m_ReportCharacter;
 
}

void CReportCtrl::SetReportCharacter(short nNewValue) 
{
	// TODO: Add your property handler here
	 m_ReportCharacter= nNewValue;
 	SetModifiedFlag();
}

void CReportCtrl::OnGroupPropertyChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CReportCtrl::OnFilePathChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

BSTR CReportCtrl::GetErrorInformation() 
{
	 
	// TODO: Add your dispatch handler code here

	return m_Errorinformation[m_ErrorinformationCode].AllocSysString();
}

bool CReportCtrl::IsFileExist(CString name,CString dirname)
{ 
WIN32_FIND_DATA fd; 
	bool IsFind=false;
    HANDLE hFind = FindFirstFile(
		dirname.GetLength ()?dirname+'\\'+name:name,&fd); 
    if ((hFind != INVALID_HANDLE_VALUE) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    IsFind=true;
    FindClose(hFind); 
  return  IsFind;
 
}

bool CReportCtrl::IsDirExist(CString name,CString parentdirname)
{ 
	 if((name.GetLength()==2&&name[1]==':')||
		 (name.GetLength()==3&&name[1]==':'&&name[2]=='\\')
		 )
     if(GetDriveType(name)!=DRIVE_NO_ROOT_DIR) return true;
	
WIN32_FIND_DATA fd; 
	bool IsFind=false;
    HANDLE hFind = FindFirstFile(
		parentdirname.GetLength ()?parentdirname+'\\'+name:name, &fd); 
    if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    IsFind=true;
    FindClose(hFind); 
  return  IsFind;

}



bool CReportCtrl::IsLegalFileName(CString name,bool wildcard )
{
	int i,j,k;
	k=wildcard?2:0;
if(	name.Find('.',0)<=0)return false;
const  char IlegalChar[]={'*','?','/','\\','<','>',':','|','\"'};
int strlen=name.GetLength ();
if(strlen>_MAX_FNAME||strlen<1) return false;
for( i=0;i<strlen;i++)
{
	for(j=k;j<sizeof(IlegalChar);j++)
		if(name[i]==IlegalChar[j]) return false;
}
return true;
}

bool CReportCtrl::grouping()
{
if(m_ReportType==OneGroupRecordOneReport||
   m_ReportType==OneTableOneReport&&((m_ReportCharacter&0x00000004)==0x00000004))
   return true;
return false;
}








void CReportCtrl::OnDataSourceKeyChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

int CReportCtrl::ReMoveChar(CString &str, char ch)
{
    int count=0;
	for(int i=0;i<str.GetLength();i++)
	{if(str[i]==ch) {str.Delete (i);i--;count++;}}
	return count;
}

int CReportCtrl::ExtractSubtring(CString &str, CStringArray &strarr)
{
CString tempstr;
int find=-1,oldfind=-1,count=0;
while((find=str.Find(',',find+1))!=-1)
{
//tempstr.Empty();
tempstr=str.Mid (oldfind+1,find-oldfind-1);
 
ReMoveChar (tempstr,' ');
if(tempstr.GetLength ()!=0)
{count++;
	strarr.Add (tempstr);
}
oldfind=find;

}
tempstr=str.Mid  (oldfind+1,str.GetLength ()-oldfind);
ReMoveChar (tempstr,' ');
if(tempstr.GetLength ()!=0)
{strarr.Add (tempstr);
count++;}
return count;
}




int CReportCtrl::GetDataSourceProperty(CStringArray &prostrarr)
{
	Range range,cell;
if(excel.m_lpDispatch ==NULL)
{
	if(!excel.CreateDispatch ("Excel.Application",NULL)) return -1;
  
	books=excel.GetWorkbooks ();
	if(books.m_lpDispatch ==NULL) return -1;
	books.Open (m_filePath+'\\'+m_DateSourceName,vtMissing,vtMissing,vtMissing,
			vtMissing,vtMissing,vtMissing,vtMissing,
			vtMissing,vtMissing,vtMissing,vtMissing,
			vtMissing);
    book=books.GetItem (_variant_t((long)1));
	if(book.m_lpDispatch ==NULL) return-1;
    sheets=book.GetSheets ();
if(sheets.m_lpDispatch ==NULL) return-1;
    worksheet=sheets.GetItem (_variant_t((long)1));
	if(worksheet.m_lpDispatch ==NULL) return-1;
}
  /*  range=worksheet.GetRows ();
    range=range.GetItem (_variant_t((long)1),vtMissing).pdispVal ;
    range=range.GetCells ();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -