手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#利用com操作excel释放进程的解决方法
C#利用com操作excel释放进程的解决方法
摘要:第一个复制代码代码如下:System.Runtime.InteropServices.Marshal.ReleaseComObject(sh...

第一个

复制代码 代码如下:

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);

System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

excelApp = null;

wbclass = null;

sheets = null;

worksheet = null;

range = null;

GC.Collect();

GC.WaitForPendingFinalizers();

释放不彻底,还是有进程存在。

第二种

复制代码 代码如下:

//调用底层函数获取进程标示

[DllImport("User32.dll")]

public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);

private static void KillExcel(Microsoft.Office.Interop.Excel.Application theApp)

{

int id = 0;

IntPtr intptr = new IntPtr(theApp.Hwnd);

System.Diagnostics.Process p = null;

try

{

GetWindowThreadProcessId(intptr, out id);

p = System.Diagnostics.Process.GetProcessById(id);

if (p != null)

{

p.Kill();

p.Dispose();

}

}

catch (Exception ex)

{

}

}

这个方法比较好,我试过了可以关闭掉进程。

【C#利用com操作excel释放进程的解决方法】相关文章:

在C#中创建和读取XML文件的实现方法

提示出现unresolved external symbol _main的解决方法

C# 委托(跨窗体操作控件)实例流程讲解

用C#对ADO.NET数据库完成简单操作的方法

在Form_Load里面调用Focus无效的解决方法

使用VS2005自带的混淆器防止你的程序被反编译的方法

C#操作注册表的方法

深入C# winform清除由GDI绘制出来的所有线条或图形的解决方法

C#中控制远程计算机的服务的方法

C#图片压缩的实现方法

精品推荐
分类导航