在VC中启动了EXCEL实例,想使用流方式打开EXCEL文件。在网上看到了DSOFRAME的源程序,里面有使用流方式打开excel文件的。自己大约按里面的思路,自己做了一个,但并不能让已启动的excel打开文件。下面是源程序,还有什么问题?
Workbooks xlBooks;
_Workbook xlBook;
CLSID clsid;
COleException e;
HRESULT hr = S_FALSE;
IStorage *pstg = NULL;
IPersistStorage *pipstg = NULL;
IOleObject* pole = NULL;
DWORD dwBindFlgs;
dwBindFlgs = (STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE);
if (FAILED(GetClassFile(L"c:\\1.xls", &clsid)))
return ;
try{
if (m_ExcelApplication.CreateDispatch( "Excel.Application" , &e) == 0)
{
AfxMessageBox( "Can't launch Excel!" );
return;
}
m_ExcelApplication.SetVisible(true);
m_ExcelApplication.SetUserControl(true);
if (SUCCEEDED(hr =xlBooks.m_lpDispatch->QueryInterface(IID_IPersistStorage, (void**)&pipstg)))
{
if (SUCCEEDED(hr = StgOpenStorage(L"c:\\2.xls", NULL, dwBindFlgs, NULL, 0, &pstg)))
{
if (SUCCEEDED(hr)) {
pstg->SetClass(clsid);
}else{
AfxMessageBox("set class type error!");
return ;
}
}else{
AfxMessageBox("open excel file error");
return ;
}
hr = pipstg->Load(pstg);
if (FAILED(hr)) {
AfxMessageBox("load stroage error");
return ;
}
pipstg->Release();
}
}
catch(COleException *e)
{
e->ReportError();
e->Delete();
return;
}
运行都没有问题,excel也是启动了,只是excel 未能打开2.xls文件
Workbooks xlBooks;
_Workbook xlBook;
CLSID clsid;
COleException e;
HRESULT hr = S_FALSE;
IStorage *pstg = NULL;
IPersistStorage *pipstg = NULL;
IOleObject* pole = NULL;
DWORD dwBindFlgs;
dwBindFlgs = (STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE);
if (FAILED(GetClassFile(L"c:\\1.xls", &clsid)))
return ;
try{
if (m_ExcelApplication.CreateDispatch( "Excel.Application" , &e) == 0)
{
AfxMessageBox( "Can't launch Excel!" );
return;
}
m_ExcelApplication.SetVisible(true);
m_ExcelApplication.SetUserControl(true);
if (SUCCEEDED(hr =xlBooks.m_lpDispatch->QueryInterface(IID_IPersistStorage, (void**)&pipstg)))
{
if (SUCCEEDED(hr = StgOpenStorage(L"c:\\2.xls", NULL, dwBindFlgs, NULL, 0, &pstg)))
{
if (SUCCEEDED(hr)) {
pstg->SetClass(clsid);
}else{
AfxMessageBox("set class type error!");
return ;
}
}else{
AfxMessageBox("open excel file error");
return ;
}
hr = pipstg->Load(pstg);
if (FAILED(hr)) {
AfxMessageBox("load stroage error");
return ;
}
pipstg->Release();
}
}
catch(COleException *e)
{
e->ReportError();
e->Delete();
return;
}
运行都没有问题,excel也是启动了,只是excel 未能打开2.xls文件
// Instantiate Excel and open an existing workbook.
objApp.CreateDispatch("Excel.Application");
objBooks = objApp.GetWorkbooks();
objBook = objBooks.Open("C:\\Test.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
谢了
你的方法是没有问题的,只是我想用流的方式打开。主要是为了能直接从数据库中直接打开EXCEL文件,而不用先保存在本地硬盘再打开
hr = pipstg->Load(pstg);
之后,调用
OleRun(pole); // IOleObject* pole = NULL; 能得到这个接口吧?
OleRun(pole); // IOleObject* pole = NULL; 能得到这个接口吧?可以取到IOleObject* pole 这个接口,运行了OleRun(pole);也不行。
在dsoframe的源代码中,也是先取到IOleObject* pole 接口,再从IOleObject* pole 接口取得IID_IPersistStorage接口的,但我还是打不开文件。
if (SUCCEEDED(hr =xlBooks.m_lpDispatch->QueryInterface(IID_IPersistStorage, (void**)&pipstg)))上面的源程序错了应该是:if (SUCCEEDED(hr =m_ExcelApplication.m_lpDispatch->QueryInterface(IID_IPersistStorage, (void**)&pipstg)))
_Workbook xlBook;
CLSID clsid;
COleException e;
HRESULT hr = S_FALSE;
IStorage *pstg = NULL;
IPersistStorage *pipstg = NULL;
IOleObject* pole = NULL;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
IPersistFile *pipf;
DWORD dwBindFlgs;
dwBindFlgs = (STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE);
if (FAILED(GetClassFile(L"c:\\2.xls", &clsid)))
return ;
try{
if (m_ExcelApplication.CreateDispatch( "Excel.Application" , &e) == 0)
{
AfxMessageBox( "Can't launch Excel!" );
return;
}
m_ExcelApplication.SetVisible(true);
m_ExcelApplication.SetUserControl(true);
if (FAILED(hr =m_ExcelApplication.m_lpDispatch->QueryInterface(IID_IOleObject, (void**)&pole)))
{
AfxMessageBox("can not find IID_IOleObject");
return ;
}
if (SUCCEEDED(hr =pole->QueryInterface(IID_IPersistStorage, (void**)&pipstg)))
{
if (SUCCEEDED(hr = StgOpenStorage(L"c:\\2.xls", NULL, dwBindFlgs, NULL, 0, &pstg)))
{
if (SUCCEEDED(hr)) {
pstg->SetClass(clsid);
}else{
AfxMessageBox("set class type error");
return ;
}
}else{
AfxMessageBox("can not open excel file");
return ;
}
hr = pipstg->Load(pstg);
if (FAILED(hr)) {
AfxMessageBox("load storage error ");
return ;
}
OleRun(pole);
pipstg->Release();
}
}
catch(COleException *e)
{
e->ReportError();
e->Delete();
return;
}
改成这样也不行
-----
不好意思 没看清楚要求,没流方式打开excel的,我倒可以给你个流方式运行exe的demo .没尝试这么BT的方法..