我在服务器生成EXCEL文件,当执行下面的代码时,在服务器上就会生成开一个EXCEL.EXE进程
我想在操作完成时把这个进程关闭,该怎么关闭啊!!                       Excel.ApplicationClass myExcel = new Excel.ApplicationClass();            Excel._Workbook xBk;       //工作薄 
            Excel._Worksheet xSt;      //工作Sheet  
                   :
                   :
                   :
           
            xBk.Close(false, "", null);
            myExcel.Quit();
            xBk = null;
            xSt = null;
            myExcel = null;
            GC.Collect();     //还是不能关闭啊!!大侠指点啊!!

解决方案 »

  1.   

    myExcel.quit(//默认参数)
      

  2.   

    GC.Collect();不能?  [DllImport("User32.dll",   CharSet   =   CharSet.Auto)]   
      public   static   extern   int   GetWindowThreadProcessId(IntPtr   hwnd,   out   int   ID);   
     protected   void   Button1_Click(object   sender,   EventArgs   e)   
      {   
          Excel.ApplicationClass   excel   =   new   Microsoft.Office.Interop.Excel.ApplicationClass();   
          excel.Workbooks.Open("d:\aaa.xls",   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing,   Type.Missing);   
          IntPtr   t   =   new   IntPtr(excel.Hwnd);   
          int   k   =   0;   
          GetWindowThreadProcessId(t,   out   k);   
          System.Diagnostics.Process   p   =   System.Diagnostics.Process.GetProcessById(k);   
          p.Kill();                   
       }
      

  3.   

      myExcel.Quit();
      System.GC.Collect();
      

  4.   

     这个问题是老问题了,应该说是.net和office有点冲突,目前没有完美的解决办法,微软公布的解决办法也是有时有效,有时无效,常用的解决方案有如下几种:
      1.使用单例模式,保证服务器上只有一个excel进程
      2. 用kill process来结束进程,但是会造成服务器上别的人就别想用excel了.
      

  5.   

    xBk = null; 
    xSt = null; 
    myExcel = null; 
    myExcel.Quit(); 顺序问题吧
      

  6.   

    这是微软公布的解决办法
    http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B317109
      

  7.   

    if (workbook != null)
    {
    workbook.Close(false,fullFileName,false);
    } //Release Excel

    if(xSt!= null)
    {
    Marshal.ReleaseComObject(xSt);
    xSt= null;
    }
    if(xBk!= null)
    {
    Marshal.ReleaseComObject(xBk);
    xBk= null;
    }
    if(myExcel != null)
    {
    Marshal.ReleaseComObject(myExcel);
    myExcel = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
      

  8.   

     找到此进程ID 然后kill掉 
      

  9.   


            /// <summary>
            /// 如何杀死word,excel等进程,下面的方法可以直接调用
             /// </summary>
            /// <param name="processName">进程名称</param>
            public void KillProcess(string processName)
            {
                System.Diagnostics.Process myproc = new System.Diagnostics.Process();            //得到所有打开的进程 
                try
                {
                    foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                    {
                        if (!thisproc.CloseMainWindow())
                        {
                            thisproc.Kill();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  10.   

        static void KillCourse(string strName)
        {
            Process[] ps = Process.GetProcesses();
            foreach (Process p in ps)
            {
                if (p.ProcessName.IndexOf(strName) >= 0)
                {
                    p.Kill();
                }
            }
        }    static void Rest()
        {
            Rest(10);
        }
        static void Rest(int second)
        {
            System.Threading.Thread.Sleep(second * 1000);
        }
      

  11.   

    在写一个过程
    比如你的是
    ExcelOut(...)
    {
     .....
     myExcel.quit()

    那么你调用这个过程的时候在写一个
    ExcelOut1(...)
    {
      ExcelOut(....);
      GC.Collect(); 
    }
    就可以关闭Excel进程了
      

  12.   

    static void KillCourse(string strName)
    这个参数直接给“EXCEL.EXE”就可以了吗???
      

  13.   

    还有如果是KILL进程的话,是不是服务器上所有的EXCEL都KILL了??
    这样真的可以吗?