简体   繁体   English

Embarcadero C ++构建器Excel Automation C ++不会退出

[英]Embarcadero C++ builder Excel Automation C++ does not quit

I am trying to Automate Excel so that it generates a report on a server and then emails this to a user when requested. 我正在尝试使Excel自动化,以便它在服务器上生成报告,然后在收到请求时通过电子邮件将其发送给用户。 The problem I have is that once I have created an excel document and written the data into it, the program does not not quit even after issuing the pXL->Quit(); 我的问题是,一旦创建了excel文档并将数据写入其中,即使发出pXL-> Quit(),该程序也不会退出。 command. 命令。 This is what my instructions for "closing" excel: 这是我“关闭” excel的说明:

pXL->Workbooks->Close(LCID(LOCALE_USER_DEFAULT));
pXLWSs.Reset(); //Releases Worksheets
pXLWS.Reset();//Releases Workbooks
pXL->Quit();// "Quits" Excel  ---> This does not quit excel
pXL.Reset();//Release Excel application?
CoUninitialize();//Uninitialise COMS

I have read quite a few articles and blog about this particular issue and it seems that this is because I am not freeing all the COMS associated with Excel. 我已经阅读了很多有关此特定问题的文章和博客,这似乎是因为我没有释放与Excel相关的所有COMS。 So I tried uninitialising COMS a few times. 所以我尝试了几次未初始化的COMS。 I issued the 我发布了

CoUninitialize(); 

4 times and this worked! 4次,这工作! But to my disappointment, it does not work all the time. 但令我感到失望的是,它并非始终有效。

Does anyone know how to gracefully close Excel? 有谁知道如何正常关闭Excel?

I know Microsoft does not support server side Excel Automation but I was hoping to find a work around. 我知道Microsoft不支持服务器端Excel Automation,但我希望能找到解决方法。 I am also aware that you can simply 我也知道您可以

WinExec("taskkill /F /IM \"excel.exe\"",0); 

which kills Excel altogether but I am worried about the future of the app when it will be generating quite a number of excel applications in which case I would have to kill Excel individually and it all just gets messy from there.... 这完全杀死了Excel,但我担心该应用程序的未来,因为它会生成大量的excel应用程序,在这种情况下,我将不得不单独杀死Excel,并且从那里变得一团糟。

Any help would be much appreciated! 任何帮助将非常感激!

Solved! 解决了!

So I finally came up with a way to kill what is left of Excel running in the background. 因此,我终于想出了一种方法来杀死在后台运行的Excel剩余的内容。

When I create Excel, I store its PID(Process Identifier) using: 当我创建Excel中,我使用存储其PID(进程标识符):

GetWindowThreadProcessId((HWND)pXL->Hwnd, &ExcelprocessID);

Then after writing data when its time to kill the application: 然后在写完数据的时候杀死应用程序:

pXL->Workbooks->Close(LCID(LOCALE_USER_DEFAULT));
pXLWSs.Reset();
// Better to use "Reset", which will clear all references to this pointer
// rather than "Release", which will only clear the last reference
pXLWS.Reset();
pXL->Quit();
pXL.Reset();
CoUninitialize();

if ( ExcelprocessID == 0 )
{
    ShowMessage("Could not find Excel ProcessID to close");
}

else
{
    HANDLE tmpHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, ExcelprocessID);
    if (NULL != tmpHandle)
    {
        TerminateProcess(tmpHandle, 0);
    }
}

Hope this helps anyone else who's been looking for a solution.... 希望这对正在寻找解决方案的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM