先把application下面的所有引用的对象依次close并且用System.Runtime.InteropServices.Marshal.ReleaseComObject(object);
回收掉,最后在app.quit()
还有在webform上操作excel不太稳定,只要出现异常excel进程就会滞留内存,尽量不要采用这种方式调用吧,使用后台进程能100%干掉进程

解决方案 »

  1.   

    Microsoft solution:
    -------------------------
    http://support.microsoft.com/?id=317109
    ---------------------------------
    Microsoft.Office.Interop.Word.Application oApp =null ;
    Microsoft.Office.Interop.Word.Document oDoc =null;

    try
    {
    oApp = new Microsoft.Office.Interop.Word.Application();
    // oApp.Visible = false;
    oDoc = new Microsoft.Office.Interop.Word.DocumentClass();
    oApp.Documents.Open2000(ref fileName,
    ref optional ,ref optional,ref optional,ref optional,ref optional,ref optional,
    ref optional,ref optional,ref optional,ref optional,ref isVisible);
    oDoc.Activate();
    }
    catch(ComException e1)
    {
    }
    finally
    {
    ReleaseObject(oDoc);
    oApp.Quit(ref optional,ref optional,ref optional);
    ReleaseObject(oApp);
    }

    }
    private string ReleaseObject(object o)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    }
    catch(Exception e)
    {
    return "release object failure:" + e.Message;
    }
    finally
    {
    o = null;
    }
    return null;
    }
    -------------------
    用try ...finally 可以确保发生异常时 释放 Office COM 对象
      

  2.   

    修改:
    private string ReleaseObject(object o)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    }
    catch(Exception e)

                       o = null;  //<---- add this statement
    return "release object failure:" + e.Message;
    }
    finally
    {
    o = null;
    }
    return null;
    }
      

  3.   

    =null后excel进程也不一定消失,还会在内存里滞留,这只有在webForm里调用会出现这样的情况