我在网上找到了一种杀死Excel 进程的方法,如下: 
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets); 
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); 
... 
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);  
System.Runtime.InteropServices.Marshal.ReleaseComObject(range); 
worksheets=null;worksheet=null; ...  
excelApp=null; 
range=null; 
 
把操作Excel文件用到的对象实例全部释放。
然后资源回收! 
GC.Collect();
说要写到finally中,我写了,可是编译时说worksheets 什么的都不存在,我想可能是需要先定义,可是我不知道具体的意思,请高手帮我解释一下具体操作的方法以及代码的意思。

解决方案 »

  1.   

    在WinApp中打开Excel,然后释放其对象并不能完全关闭,需要用Process.Kill来强制关闭。
      

  2.   

    那么请问我应该如何写我的kill excel.dll进程的代码?
      

  3.   

    reference:
    http://www.codeproject.com/csharp/package.asp
      

  4.   

    用Process.Kill方法肯定不行,因为这样会将正在应用的进程也杀掉,容易出现异常。你报错是因为虽然你定义了,但没有初始化,当然不行。你要加保护:
    myApp=new Excel.ApplicationClass();
    myApp.Visible = false;
    oMissiong = System.Reflection.Missing.Value;
    myBooks = myApp.Workbooks;
    myBook = myBooks.Open(ExcelFile,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
    mySheet=(Excel.Worksheet)myBook.Sheets[1];
    ..........................
    .........................
    if(myBook != null)
    myBook.Close(true, ExcelFile, oMissiong); // Quit Excel and clean up.
    if(mySheet != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject (mySheet);
    mySheet = null;
    if(myBook != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject (myBook);
    myBook = null;
    if(myBooks != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject (myBooks);
    myBooks = null;
    if(myApp != null)
    {
    myApp.Workbooks.Close();
    myApp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject (myApp);
    myApp = null;
    }
    GC.Collect();
    }
      

  5.   

    这不是进程的问题.
    参照这个看一下
    http://community.csdn.net/Expert/topic/4788/4788931.xml?temp=4.803103E-02