基本上我有一个用C#编写的程序,它使用excel应用程序。

在运行程序时,如果用户尝试打开任何excel文件,则会在由我的程序创建的excel实例上打开该文件。

我想这与excel打开现有的实例有关
“ Excel.exe”。

当我的程序退出时,它会导致僵尸excel进程。

我想做的是隐藏我的excel实例,以免被用户使用。
(或任何其他解决方案)

我试图用谷歌搜索出一些东西,但到目前为止没有。

我的代码如下所示。

foreach (string strFileName in this.m_ListRequestFileNames)
{
    object misVal = System.Reflection.Missing.Value;
    try
    {
        if (Common.IsOpened(strFileName) == true)
        {
            this.m_ParentWnd.Log(strFileName + " is in use by another program." + Environment.NewLine);
            continue;
        }
        this.m_xlWBook = this.m_xlApp.Workbooks.Open(strFileName, 3, true, misVal, misVal, misVal, misVal, misVal, misVal, false, misVal, misVal, misVal, misVal, misVal);
        if (Make(ref m_xlWBook, m_strDstPath, ref m_ListIgnoreSheetNames) != true)
        {
            this.m_ParentWnd.Log("-!- Error while making " + m_xlWBook.Name + " into a csv file" + Environment.NewLine);
            continue;
        }
        this.m_ParentWnd.Log("Completed " + m_xlWBook.Name + Environment.NewLine);
    }
    catch (System.Exception e)
    {
        m_ParentWnd.Log(e.ToString());
    }
    this.m_xlWBook.Close(false, null, null);
}
this.m_xlApp.Quit();
Common.releaseObject(this.m_xlApp);

最佳答案

问题,至少从我从代码中看到的问题是,一旦完成(app.Quit),您就不会关闭Excel-如果这样做,您可能就不会处置所创建的对象。使用Excel interop,您将无法进行愉快的垃圾收集,因此您必须小心自己进行清理。一个很小的被遗忘的实例将使Excel在后台运行,并带有您描述的烦人的副作用。请查看此StackOverflow线程,以对此进行广泛的讨论:
How do I properly clean up Excel interop objects?

关于c# - C#如何阻止用户使用程序创建的Excel应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3340633/

10-09 07:02
查看更多