Ole与ActiveX

lymusic2007 2010-04-02 12:43:30
//---------------------------------------------------------------------------


#pragma hdrstop

#include "DExcelBrokerBase.h"

//---------------------------------------------------------------------------
//构造函数
//只读、只写、读写都可以
DExcelBrokerBase::DExcelBrokerBase( const AnsiString &sFilePath, const EConType eType )
{
switch( eType )
{
case EXCRead:
s_gFileReadPath = sFilePath;
break;
case EXCWrite:
s_gFileWritePath = sFilePath;
break;
case EXCAll:
s_gFileReadPath = sFilePath;
s_gFileWritePath = sFilePath;
break;
default:
break;
}
}

//---------------------------------------------------------------------------
//读写同时
DExcelBrokerBase::DExcelBrokerBase( const AnsiString &sFileReadPath, const AnsiString &sFileWritePath )
{
s_gFileReadPath = sFileReadPath;
s_gFileWritePath = sFileWritePath;
}


//---------------------------------------------------------------------------
//析构函数
DExcelBrokerBase::~DExcelBrokerBase()
{

//
if ( !v_gEReadApp.IsEmpty() )
{
CloseReadObj();
v_gEReadApp = NULL;
}
if ( !v_gEReadCell.IsEmpty() )
{
v_gEReadCell.Clear();
v_gEReadCell = NULL;
}
if ( !v_gEReadSheet.IsEmpty() )
{
v_gEReadSheet.Clear();
v_gEReadSheet = NULL;
}

if ( !v_gEReadSingleBook.IsEmpty() )
{
v_gEReadSingleBook.Clear();
v_gEReadSingleBook = NULL;
}

if ( !v_gEReadAllBooks.IsEmpty() )
{
v_gEReadAllBooks.Clear();
v_gEReadAllBooks = NULL;
}

//
if ( !v_gEWriteApp.IsEmpty() || !v_gEWriteSingleBook.IsEmpty() )
{
CloseWriteObj();
v_gEWriteSingleBook = NULL;
v_gEWriteApp = NULL;
}

if ( !v_gEWriteCell.IsEmpty() )
{
v_gEWriteCell.Clear();
v_gEWriteCell = NULL;
}
if ( !v_gEWriteSheet.IsEmpty() )
{
v_gEWriteSheet.Clear();
v_gEWriteSheet = NULL;
}
if ( !v_gEWriteAllBooks.IsEmpty() )
{
v_gEWriteAllBooks.Clear();
v_gEWriteAllBooks = NULL;
}

}

///////////////////////////////////////////////////////////////////////////
//创建OLE对象
int DExcelBrokerBase::CreateExcReadObj()
{
try
{
v_gEReadApp = CreateOleObject("Excel.Application");
}
catch( Exception &E )
{
s_gErrMsg = " 创建OLE对象失败!";
return EXC_CREATE_OBJ_ERR;
}
return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//初始化读EXCEL文件对象的属性
int DExcelBrokerBase::InitRead()
{
int iRtn;
iRtn = CreateExcReadObj();
if( iRtn != EXC_SUCC )
{
return iRtn;
}

try
{
v_gEReadApp.OlePropertySet( "Visible", (Variant)false );

v_gEReadAllBooks = v_gEReadApp.OlePropertyGet( "Workbooks" );
v_gEReadSingleBook = v_gEReadAllBooks.OleFunction("Open", s_gFileReadPath.c_str() );

v_gEReadSheet = v_gEReadSingleBook.OlePropertyGet("ActiveSheet"); //此行可否不要

v_gEReadCell = v_gEReadApp.OlePropertyGet("ActiveCell").OleFunction( "SpecialCells", 11 );

i_gColumn = v_gEReadCell.OlePropertyGet("Column"); //返回列
i_gRow = v_gEReadCell.OlePropertyGet("Row");

}
catch( Exception &Excp )
{
s_gErrMsg = "设置OLE对象属性出错!\n" + Excp.Message;

iRtn = CloseReadObj();
if ( iRtn != EXC_SUCC )
{
return iRtn;
}

return EXC_INIT_ERR;
}
return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//读表格
int DExcelBrokerBase::ReadSingleCell( const int iRow, const int iColumn, AnsiString &sValue )
{
int iRtn;
try
{
sValue = v_gEReadApp.OlePropertyGet("Cells", iRow, iColumn).OlePropertyGet("Value");
if ( iColumn == 7 ) //取身份证前18位
{
sValue = sValue.Trim();
sValue = sValue.SubString( 0, 18 );
}
}
catch( Exception &Ex )
{
s_gErrMsg = " 读数据失败!\n" + Ex.Message;
iRtn = CloseReadObj();
if ( iRtn != EXC_SUCC )
{
return iRtn;
}

return EXC_READ_CELL_ERR;
}

return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//关闭读对象
int DExcelBrokerBase::CloseReadObj()
{
try
{

v_gEReadApp.OleFunction("Quit");
}
catch( Exception &Ex )
{
s_gErrMsg = " 关闭读EXCEL对象失败!\n" + Ex.Message;
return EXC_QUIT_ERR;
}
return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//创建OLE写的对象
int DExcelBrokerBase::CreateExcWriteObj()
{
try
{
v_gEWriteApp = CreateOleObject("Excel.Application");
}
catch( Exception &E )
{
s_gErrMsg = " 创建OLE对象失败!\n" + E.Message;
return EXC_CREATE_OBJ_ERR;
}
return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//设置OLE对象属性
int DExcelBrokerBase::InitWrite()
{
int iRtn;

iRtn = CreateExcWriteObj();
if ( iRtn != EXC_SUCC )
{
return iRtn;
}

try
{
v_gEWriteApp.OlePropertySet("Visible", (Variant)false );
v_gEWriteSingleBook = (v_gEWriteApp.OlePropertyGet("workbooks")).OleFunction("Add"); //为什么写时用 v_gEWriteApp
}
catch( Exception &E )
{
s_gErrMsg = " 初始化写属性失败!" + E.Message;
iRtn = CloseWriteObj();
if ( iRtn != EXC_SUCC )
{
return iRtn;
}
return EXC_INIT_ERR;
}

return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//写CELL表格
int DExcelBrokerBase::WriteSingleCell( const int iRow, const int iColumn, AnsiString &sValue )
{
int iRtn;
try
{
v_gEWriteApp.OlePropertyGet("Cells", iRow, iColumn ).OlePropertySet("Value", sValue.c_str() );

}
catch( Exception &E )
{
s_gErrMsg = " 写EXCLE表格失败!" + E.Message;
iRtn = CloseWriteObj();
if ( iRtn != EXC_SUCC )
{
return iRtn;
}
return EXC_WRITE_ERR;
}

return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//关闭写对象
int DExcelBrokerBase::CloseWriteObj()
{
try
{
v_gEWriteSingleBook.OleFunction("SaveAs", s_gFileWritePath.c_str() ); //需要测试
v_gEWriteApp.OleFunction("Quit");
}
catch( Exception &Ex )
{
s_gErrMsg = " 关闭写EXCLE对象失败!" + Ex.Message;
return EXC_QUIT_ERR;
}
return EXC_SUCC;
}

///////////////////////////////////////////////////////////////////////////
//关闭读与写的对象
int DExcelBrokerBase::CloseRWObj( EConType eType )
{
int iRtn;
switch( eType )
{
case EXCRead:
iRtn = CloseReadObj();
break;
case EXCWrite:
iRtn = CloseWriteObj();
break;
case EXCAll:
iRtn = CloseReadObj();
if ( iRtn == EXC_SUCC )
{
iRtn = CloseWriteObj();
}
break;
default:
break;
}
return iRtn;
}

///////////////////////////////////////////////////////////////////////////
//设置OLE对象属性类型
int DExcelBrokerBase::Init( EConType eType )
{
int iRtn;
switch( eType )
{
case EXCRead:
iRtn = InitRead();
break;
case EXCWrite:
iRtn = InitWrite();
break;
case EXCAll:
iRtn = InitRead();
if ( iRtn == EXC_SUCC )
{
iRtn = InitWrite();
}
default:
break;
}
return iRtn;
}

///////////////////////////////////////////////////////////////////////////
#pragma package(smart_init)

当调用到: v_gEReadApp.OlePropertySet( "Visible", (Variant)false ); 时出现

内存弹框出来!

这代码我测试程序中全部都通过了,可一加入到自己正式环境内,出问题了,网上查了一些资料与ActiveX控件相冲突,

有谁能指点指点吗??
...全文
68 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangfei19890914 2010-10-12
  • 打赏
  • 举报
回复
没人回答么?我遇到了同样的问题。
lymusic2007 2010-04-02
  • 打赏
  • 举报
回复
Access violation at address 0054D4CF , read of address 00000800

1,194

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 Informix
社区管理员
  • Informix社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧