VC通过COM组件对Excel 进行读写
下载地址:http://download.csdn.net/source/1850105
//*******************************************************************************
// FILE NAME : cExcel.h
//
// DESCRIPTION : a class for control excel file
// compile with VC 6.0
//
// CREATE DATE : 2009-11-17
// AUTHOR : 牧笛Andy
// QQ : 525908322
// http://hi.baidu.com/andy84/
//*******************************************************************************
#ifndef __CEXCEL_H__
#define __CEXCEL_H__
#include "stdafx.h"
//import mso.dll
#import "C:/Program Files/Common Files/Microsoft Shared/OFFICE11/mso.dll" /
rename("RGB", "MSRGB")
//import VBE6EXT.OLB
#import "C:/Program Files/Common Files/Microsoft Shared/VBA/VBA6/VBE6EXT.OLB" raw_interfaces_only, /
rename("Reference", "ignorethis"),/
rename("VBE", "JOEVBE")
//import excel.exe
#import "C:/Program Files/Microsoft Office/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")
using namespace Office;
using namespace VBIDE;
using namespace Excel ;
const COleVariant covTrue((short)TRUE);
const COleVariant covFalse((short)FALSE);
const COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
class cExcel
{
public:
cExcel();
~cExcel();
void StartApp(void);
void CreateWorkBook(void);
void OpenWorkBook(CString filename);
void SelectAcitveSheet(void);
void SetSheetName(CString sheetname);
void SelectRange( CString range );
void SelectRange( int x , int y );
void SetBackColor( long color );
void GetFont(void);
void SetFontName(CString fontname);
void SetFontSytle(CString fontsytle);
void SetFontSize(long size);
void PutValue(CString string);
void PutValue(int x , int y , CString string );
CString GetCString( int x , int y );
double GetValue(int x , int y );
void Save(CString filename);
void Save( CString path , CString filename );
void Quit(void);
private:
_ApplicationPtr pApp;
WorkbooksPtr pBooks ;
_WorkbookPtr pBook ;
SheetsPtr pSheets ;
_WorksheetPtr pSheet;
RangePtr pRange;
InteriorPtr pInterior;
FontPtr pFont;
};
#endif //CEXCEL_H
//*******************************************************************************
// FILE NAME : cExcel.cpp
//
// DESCRIPTION : a class for control excel file
// compile with VC 6.0
//
// CREATE DATE : 2009-11-17
// AUTHOR : 牧笛Andy
// QQ : 525908322
// http://hi.baidu.com/andy84/
//*******************************************************************************
#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 : open excel.exe
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::StartApp(void)
{
CoInitialize ( NULL );
pApp.CreateInstance(L"Excel.Application");
pApp->PutVisible(0,VARIANT_TRUE);//VARIANT_TRUE
pBooks = pApp->GetWorkbooks();
}
//*******************************************************************************
// FUNCTION : CreateWorkBook
// DESCRIPTION : create *.xls file
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::CreateWorkBook(void)
{
assert(pBooks != NULL );
pBook = pBooks->Add((long)xlWorkbook);
}
//*******************************************************************************
// FUNCTION : OpenWorkBook
// DESCRIPTION : open *.xls file
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::OpenWorkBook(CString filename )
{
assert(pBooks != NULL );
pBook = pBooks->Open( (LPCSTR)filename );
}
//*******************************************************************************
// FUNCTION : SelectAcitveSheet
// DESCRIPTION :
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::SelectAcitveSheet(void)
{
assert(pBook != NULL );
pSheets = pBook->GetWorksheets();
pSheet = pBook->GetActiveSheet();
}
//*******************************************************************************
// FUNCTION : SetSheetName
// DESCRIPTION :
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::SetSheetName(CString sheetname)
{
assert(pSheet != NULL );
pSheet->PutName( (LPCSTR)sheetname);
}
//*******************************************************************************
// FUNCTION : SelectRange
// DESCRIPTION :
//
// PARAMETERS : CString range (eg. "A1")
// RETURN : void
//*******************************************************************************
void cExcel::SelectRange(CString range )
{
assert(pSheet != NULL );
pRange = pSheet->GetRange((LPCSTR) range ,vtMissing);
}
//*******************************************************************************
// FUNCTION : SelectRange
// DESCRIPTION :
//
// PARAMETERS : int x , int y
// RETURN : void
//*******************************************************************************
void cExcel::SelectRange( int x , int y )
{
CString range ;
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 : long color
// RETURN : void
//*******************************************************************************
void cExcel::SetBackColor( long color )
{
assert(pRange != NULL );
pInterior = pRange->GetInterior();
pInterior->PutColor((long )color);
}
//*******************************************************************************
// FUNCTION : GetFont
// DESCRIPTION :
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::GetFont(void)
{
assert(pFont != NULL );
pFont =pRange->GetFont();
}
//*******************************************************************************
// FUNCTION : SetFontName
// DESCRIPTION :
//
// PARAMETERS : CString fontname
// RETURN : void
//*******************************************************************************
void cExcel::SetFontName( CString fontname)
{
assert(pFont != NULL );
pFont->PutName((LPCSTR)fontname);//(L"隶书")
}
//*******************************************************************************
// FUNCTION : SetFontSytle
// DESCRIPTION :
//
// PARAMETERS : CString fontsytle
// RETURN : void
//*******************************************************************************
void cExcel::SetFontSytle( CString fontsytle )
{
assert(pFont != NULL );
pFont->PutFontStyle((LPCSTR)fontsytle);//(L"Bold Italic")
}
//*******************************************************************************
// FUNCTION : SetFontSize
// DESCRIPTION :
//
// PARAMETERS : long size
// RETURN : void
//*******************************************************************************
void cExcel::SetFontSize( long size )
{
assert(pFont != NULL );
pFont->PutSize((long)size);
}
//*******************************************************************************
// FUNCTION : PutValue
// DESCRIPTION :
//
// PARAMETERS : CString string
// RETURN : void
//*******************************************************************************
void cExcel::PutValue( CString string )
{
assert(pRange != NULL );
pRange->PutValue2((LPCSTR)string );
}
//*******************************************************************************
// FUNCTION : PutValue
// DESCRIPTION :
//
// PARAMETERS : int x , int y , CString string
// RETURN : void
//*******************************************************************************
void cExcel::PutValue( int x , int y , CString string )
{
SelectRange(x, y);
PutValue( string );
}
//*******************************************************************************
// FUNCTION : PutValue
// DESCRIPTION : read sting
//
// PARAMETERS : int x , int y
// RETURN : CString
//*******************************************************************************
CString cExcel::GetCString( int x , int y )
{
CString result;
COleVariant vResult;
SYSTEMTIME st ;
CString stry ,strm ,strd ;
SelectRange(x, y);
assert(pRange != NULL );
///
vResult = pRange->GetValue2();
switch ( vResult.vt )
{
case VT_BSTR: //OLE Automation string
{
result = vResult.bstrVal;
break;
}
case VT_R8 : // 8 byte real
{
result.Format("%f",vResult.dblVal);
break;
}
case VT_DATE: //date
{
VariantTimeToSystemTime(vResult.date, &st);
stry.Format("%d",st.wYear);
strm.Format("%d",st.wMonth);
strd.Format("%d",st.wDay);
result = stry+"-"+strm+"-"+strd;
break ;
}
case VT_EMPTY: //empty
{
result.Empty();
break ;
}
default:
{
AfxMessageBox("not string ");
result.Empty();
break;
}
}
return result;
}
//*******************************************************************************
// FUNCTION : GetValue
// DESCRIPTION :
//
// PARAMETERS : int x , int y
// RETURN : double
//*******************************************************************************
double cExcel::GetValue(int x , int y )
{
double result ;
COleVariant vResult;
SelectRange(x, y);
vResult = pRange->GetValue2();
if(VT_R8 == vResult.vt)
{
result = vResult.dblVal ;
}
else
{
AfxMessageBox(" not VT_R8 ");
}
return result ;
}
//*******************************************************************************
// FUNCTION : Save
// DESCRIPTION :
//
// PARAMETERS : CString filename
// RETURN : void
//*******************************************************************************
void cExcel::Save(CString filename)
{
TCHAR szFilePath[MAX_PATH + 1];
CString str_url ;
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('//')))[1] = 0;
str_url = szFilePath;
str_url += filename ;
assert(pBook != NULL );
///
pBook->SaveAs( (LPCSTR) str_url ,(long)xlNormal ,(LPCSTR)"", (LPCSTR)"" ,(long)FALSE,(long)FALSE ,xlShared );
}
//*******************************************************************************
// FUNCTION : Save
// DESCRIPTION :
//
// PARAMETERS : CString path , CString filename
// RETURN : void
//*******************************************************************************
void cExcel::Save( CString path , CString filename )
{
assert(pBook != NULL );
path += filename ;
pBook->SaveAs( (LPCSTR) path ,(long)xlNormal ,(LPCSTR)"", (LPCSTR)"" ,(long)FALSE,(long)FALSE ,xlShared );
}
//*******************************************************************************
// FUNCTION : Quit
// DESCRIPTION : close excel.exe
//
// PARAMETERS : void
// RETURN : void
//*******************************************************************************
void cExcel::Quit(void)
{
assert(pBook != NULL );
assert(pBooks != NULL );
assert(pApp != NULL );
pBook->PutSaved(0,VARIANT_TRUE);//don't answer to save
pBooks->Close();
pApp->Quit();
CoUninitialize ();
}