2012-08-28 172 views
0

我曾尝试:如何完全关闭Excel应用程序/工作簿?

while (System::Runtime::InteropServices::Marshal::ReleaseComObject(worksheet_instance) > 0) ; 
workbook_instance->Close(true, "Dummy.xlsx", Missing::Value); 
while (System::Runtime::InteropServices::Marshal::ReleaseComObject(workbook_instance) > 0) ; 
workbook_instance->~Workbook(); 
exApp_instance->Quit(); 
exApp_instance->~Application(); 

但它不会终止Excel应用程序(我仍然可以看到它在任务管理器)。 作为一个尝试,我想这样做

workbook_instance = NULL; 

,但它是不能接受的。 有什么建议吗? 谢谢。

+0

成功请参考以下链接。尽管它在C#中,但你会得到一个想法。 [如何正确清理C#中的Excel互操作对象](http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c-sharp)[如何做你关闭Excel COM对象并保存工作?](http://stackoverflow.com/questions/686789/how-do-you-close-excel-com-object-and-save-the-work) –

回答

0

谢谢@amaldev
据第一个链接收集了一些想法,我

try{ 
    while (System::Runtime::InteropServices::Marshal::ReleaseComObject(ws) > 0) ; 
} catch(...) {} 

wb->Close(false, Missing::Value, Missing::Value); 
try { 
    while (System::Runtime::InteropServices::Marshal::ReleaseComObject(wb) > 0) ; 
} catch(...) {} 

exApp->Quit(); 
try { 
    while (System::Runtime::InteropServices::Marshal::ReleaseComObject(exApp) > 0) ; 
} 
catch(...) {} 

System::GC::Collect(); 
System::GC::WaitForPendingFinalizers(); 
相关问题