C读取Excel类.docx
《C读取Excel类.docx》由会员分享,可在线阅读,更多相关《C读取Excel类.docx(18页珍藏版)》请在冰豆网上搜索。
C读取Excel类
VCͨ¹ýCOM×é¼þ¶ÔExcel½øÐжÁд
ÏÂÔصØÖ·£º
//*******************************************************************************
//FILENAME:
cExcel.h
//
//DESCRIPTION:
aclassforcontrolexcelfile
//compilewithVC6.0
//
//CREATEDATE:
2009-11-17
//AUTHOR:
ÄÁµÑAndy
//QQ:
525908322
//
//*******************************************************************************
#ifndef__CEXCEL_H__
#define__CEXCEL_H__
#include"stdafx.h"
//importmso.dll
#import"C:
\ProgramFiles\CommonFiles\MicrosoftShared\OFFICE11\mso.dll"\
rename("RGB","MSRGB")
//importVBE6EXT.OLB
#import"C:
\ProgramFiles\CommonFiles\MicrosoftShared\VBA\VBA6\VBE6EXT.OLB"raw_interfaces_only,\
rename("Reference","ignorethis"),\
rename("VBE","JOEVBE")
//importexcel.exe
#import"C:
\ProgramFiles\MicrosoftOffice\OFFICE11\excel.exe"exclude("IFont","IPicture")\
rename("RGB","ignorethis"),\
rename("DialogBox","ignorethis"),\
rename("VBE","JOEVBE"),\
rename("ReplaceText","JOEReplaceText"),\
rename("CopyFile","JOECopyFile"),\
rename("FindText","JOEFindText"),\
rename("NoPrompt","JOENoPrompt")
usingnamespaceOffice;
usingnamespaceVBIDE;
usingnamespaceExcel;
constCOleVariantcovTrue((short)TRUE);
constCOleVariantcovFalse((short)FALSE);
constCOleVariantcovOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
classcExcel
{
public:
cExcel();
~cExcel();
voidStartApp(void);
voidCreateWorkBook(void);
voidOpenWorkBook(CStringfilename);
voidSelectAcitveSheet(void);
voidSetSheetName(CStringsheetname);
voidSelectRange(CStringrange);
voidSelectRange(intx,inty);
voidSetBackColor(longcolor);
voidGetFont(void);
voidSetFontName(CStringfontname);
voidSetFontSytle(CStringfontsytle);
voidSetFontSize(longsize);
voidPutValue(CStringstring);
voidPutValue(intx,inty,CStringstring);
CStringGetCString(intx,inty);
doubleGetValue(intx,inty);
voidSave(CStringfilename);
voidSave(CStringpath,CStringfilename);
voidQuit(void);
private:
_ApplicationPtrpApp;
WorkbooksPtrpBooks;
_WorkbookPtrpBook;
SheetsPtrpSheets;
_WorksheetPtrpSheet;
RangePtrpRange;
InteriorPtrpInterior;
FontPtrpFont;
};
#endif//CEXCEL_H
//*******************************************************************************
//FILENAME:
cExcel.cpp
//
//DESCRIPTION:
aclassforcontrolexcelfile
//compilewithVC6.0
//
//CREATEDATE:
2009-11-17
//AUTHOR:
ÄÁµÑAndy
//QQ:
525908322
//
//*******************************************************************************
#include"stdafx.h"
#include"cExcel.h"
#include"Assert.h"
//*******************************************************************************
//FUNCTION:
cExcel
//DESCRIPTION:
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
cExcel:
:
cExcel()
{
pApp=NULL;
pBooks=NULL;
pBook=NULL;
pSheets=NULL;
pSheet=NULL;
pRange=NULL;
pInterior=NULL;
pFont=NULL;
}
//*******************************************************************************
//FUNCTION:
~cExcel
//DESCRIPTION:
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
cExcel:
:
~cExcel()
{
}
//*******************************************************************************
//FUNCTION:
StartApp
//DESCRIPTION:
openexcel.exe
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
StartApp(void)
{
CoInitialize(NULL);
pApp.CreateInstance(L"Excel.Application");
pApp->PutVisible(0,VARIANT_TRUE);//VARIANT_TRUE
pBooks=pApp->GetWorkbooks();
}
//*******************************************************************************
//FUNCTION:
CreateWorkBook
//DESCRIPTION:
create*.xlsfile
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
CreateWorkBook(void)
{
assert(pBooks!
=NULL);
pBook=pBooks->Add((long)xlWorkbook);
}
//*******************************************************************************
//FUNCTION:
OpenWorkBook
//DESCRIPTION:
open*.xlsfile
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
OpenWorkBook(CStringfilename)
{
assert(pBooks!
=NULL);
pBook=pBooks->Open((LPCSTR)filename);
}
//*******************************************************************************
//FUNCTION:
SelectAcitveSheet
//DESCRIPTION:
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SelectAcitveSheet(void)
{
assert(pBook!
=NULL);
pSheets=pBook->GetWorksheets();
pSheet=pBook->GetActiveSheet();
}
//*******************************************************************************
//FUNCTION:
SetSheetName
//DESCRIPTION:
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SetSheetName(CStringsheetname)
{
assert(pSheet!
=NULL);
pSheet->PutName((LPCSTR)sheetname);
}
//*******************************************************************************
//FUNCTION:
SelectRange
//DESCRIPTION:
//
//PARAMETERS:
CStringrange(eg."A1")
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SelectRange(CStringrange)
{
assert(pSheet!
=NULL);
pRange=pSheet->GetRange((LPCSTR)range,vtMissing);
}
//*******************************************************************************
//FUNCTION:
SelectRange
//DESCRIPTION:
//
//PARAMETERS:
intx,inty
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SelectRange(intx,inty)
{
CStringrange;
x=(0==x)?
1:
x;
y=(0==y)?
1:
y;
x=(x>26)?
(x%26):
(x);
range.Format("%c%d",y+'A'-1,x);
SelectRange(range);
}
//*******************************************************************************
//FUNCTION:
SetBackColor
//DESCRIPTION:
//
//PARAMETERS:
longcolor
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SetBackColor(longcolor)
{
assert(pRange!
=NULL);
pInterior=pRange->GetInterior();
pInterior->PutColor((long)color);
}
//*******************************************************************************
//FUNCTION:
GetFont
//DESCRIPTION:
//
//PARAMETERS:
void
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
GetFont(void)
{
assert(pFont!
=NULL);
pFont=pRange->GetFont();
}
//*******************************************************************************
//FUNCTION:
SetFontName
//DESCRIPTION:
//
//PARAMETERS:
CStringfontname
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SetFontName(CStringfontname)
{
assert(pFont!
=NULL);
pFont->PutName((LPCSTR)fontname);//(L"Á¥Êé")
}
//*******************************************************************************
//FUNCTION:
SetFontSytle
//DESCRIPTION:
//
//PARAMETERS:
CStringfontsytle
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SetFontSytle(CStringfontsytle)
{
assert(pFont!
=NULL);
pFont->PutFontStyle((LPCSTR)fontsytle);//(L"BoldItalic")
}
//*******************************************************************************
//FUNCTION:
SetFontSize
//DESCRIPTION:
//
//PARAMETERS:
longsize
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
SetFontSize(longsize)
{
assert(pFont!
=NULL);
pFont->PutSize((long)size);
}
//*******************************************************************************
//FUNCTION:
PutValue
//DESCRIPTION:
//
//PARAMETERS:
CStringstring
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
PutValue(CStringstring)
{
assert(pRange!
=NULL);
pRange->PutValue2((LPCSTR)string);
}
//*******************************************************************************
//FUNCTION:
PutValue
//DESCRIPTION:
//
//PARAMETERS:
intx,inty,CStringstring
//RETURN:
void
//*******************************************************************************
voidcExcel:
:
PutValue(intx,inty,CStringstring)
{
SelectRange(x,y);
PutValue(string);
}
//*******************************************************************************
//FUNCTION:
PutValue
//DESCRIPTION:
readsting
//
//PARAMETERS:
intx,inty
//RETURN:
CString
//*******************************************************************************
CStringcExcel:
:
GetCString(intx,inty)
{
CStringresult;
COleVariantvResult;
SYSTEMTIMEst;
CStringstry,strm,strd;
SelectRange(x,y);
////////////////////////
assert(pRange!
=NULL);
///////////////////////
vResult=pRange->GetValue2();
switch(vResult.vt)
{
caseVT_BSTR:
//OLEAutomationstring
{
result=vResult.bstrVal;
break;
}
caseVT_R8:
//8bytereal
{
result.Format("%f",vResult.dblVal);
break;
}
caseVT_DATE:
//date
{
VariantTimeToSystemTime(vResult.date,&st);
stry.Format("%d",st.wYear);
strm.Format("%d",