C#利用com操作excel释放进程的解决方法
时间:2022-08-02 07:01:28
第一个
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)
{
}
}
这个方法比较好,我试过了可以关闭掉进程。
标签:com,excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot整合之SpringBoot整合MongoDB的详细步骤
2023-11-25 09:55:37
![](https://img.aspxhome.com/file/2023/6/59076_0s.png)
SpringBoot利用jackson格式化时间的三种方法
2022-03-26 22:28:56
![](https://img.aspxhome.com/file/2023/1/71191_0s.png)
Netty分布式NioEventLoop任务队列执行源码分析
2022-10-08 04:07:19
C# WinForm制作异形窗体与控件的方法
2023-11-07 11:09:28
Java实现双保险线程的示例代码
2023-09-21 07:59:46
详解Mybatis极其(最)简(好)单(用)的一个分页插件
2021-09-25 03:00:35
java图片识别文字的方法
2021-11-11 02:43:03
Mybatis实现自定义类型转换器TypeHandler的方法
2023-09-28 14:52:22
![](https://img.aspxhome.com/file/2023/9/80279_0s.png)
Java C++题解leetcode 1684统计一致字符串的数目示例
2023-04-23 09:06:31
![](https://img.aspxhome.com/file/2023/2/71252_0s.png)
使用C语言编写基于TCP协议的Socket通讯程序实例分享
2023-07-05 03:30:03
java用类加载器的5种方式读取.properties文件
2022-05-06 14:55:19
![](https://img.aspxhome.com/file/2023/9/75419_0s.png)
用JAVA实现杨辉三角实例
2023-08-28 16:45:23
![](https://img.aspxhome.com/file/2023/6/79896_0s.png)
浅谈Java字符串比较的三种方法
2023-05-13 12:26:51
springboot 防止重复请求防止重复点击的操作
2021-09-19 16:03:00
![](https://img.aspxhome.com/file/2023/6/71196_0s.png)
Spring Data JPA调用存储过程实例代码
2023-11-25 00:13:15
Java中单例模式的七种写法示例
2021-07-16 07:57:22
![](https://img.aspxhome.com/file/2023/9/75799_0s.png)
最小树形图模板朱刘算法分享
2023-11-07 07:04:38
Java实现计算器设计
2023-08-18 13:36:54
![](https://img.aspxhome.com/file/2023/7/79347_0s.jpg)
Maven 配置文件 生命周期 常用命令详解
2022-07-05 19:59:39
Java编程实现中英混合字符串数组按首字母排序的方法
2022-03-16 02:34:54