C#.net调用Excel出现问题

excel进程无法通过excelapp.quit()退出。

1.尝试http://support.microsoft.com/kb/317109/zh-cn中的方法,不起作用。

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();

2.Kill掉

oExcelApp.Quit();     

oExcelApp = null;     

PublicMethod.Kill(oExcelApp);//调用kill当前excel进程

PublicMethod.Kill()内容是:

using System.Runtime.InteropServices;  

public class PublicMethod  

{   

    public PublicMethod()   

    {    //    // TODO: 在此处添加构造函数逻辑    //   }   

[DllImport("User32.dll",   CharSet   =   CharSet.Auto)]      

public static extern int GetWindowThreadProcessId(IntPtr hwnd,out int ID);   

public static void Kill(Excel.Application excel)  

 {       

IntPtr t=new IntPtr(excel.Hwnd);   //得到这个句柄,具体作用是得到这块内存入口 

  int k= 0;      

 GetWindowThreadProcessId(t,out k);   //得到本进程唯一标志k    

System.Diagnostics.Process p=System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用   

 p.Kill();     //关闭进程k   

}

}

posted on 2013-08-05 19:22  亲亲柚子  阅读(414)  评论(0编辑  收藏  举报

导航