C#中通过com组件操作excel不能关闭的问题
来源:互联网 发布:js对象概念 编辑:程序博客网 时间:2024/05/17 05:05
问题:
当用如下代码操作完Excel,虽然调用了Application的Quit()方法,但发现Excel进程并没退出。
object missing = System.Reflection.Missing .Value; Application app = new Application(); app.Visible = false ; app.UserControl = true ; Workbook wb = app.Workbooks.Open(path, missing, true , missing, missing, missing, missing, missing, missing, true , missing, missing, missing, missing, missing); Worksheet ws = (Worksheet )wb.Worksheets.get_Item(1); Console .WriteLine(ws.Cells[2,1].Value); ws.Close(); ws = null; app.Quit(); app = null;
结果:
原因:
When Visual Studio .NET calls a COM object from managed code, it automatically creates a Runtime Callable Wrapper (RCW). The RCW marshals calls between the .NET application and the COM object. The RCW keeps a reference count on the COM object. Therefore, if all references have not been released on the RCW, the COM object does not quit.
解决方案:
1、为每一个对象定义一个变量。
例如:
把这段代码
Workbook wb = wbs.Open(path, missing, true , missing, missing, missing, missing,
missing, missing, true , missing, missing, missing, missing, missing);
改为
Workbooks wbs = app.Workbooks;
Workbook wb = wbs.Open(path, missing, true , missing, missing, missing, missing,
missing, missing, true , missing, missing, missing, missing, missing);
2、当使用完com对象对其循环调用System.Runtime.InteropServices.Marshal.ReleaseComObject 直到返回值为0
3、设置变量位null
4、调用Quit方法通知服务器关闭
5、调用GC .Collect();
最终:
把上边的代码修改为:
public static void ExcelRead(string path) { object missing = System.Reflection.Missing.Value; Application app = new Application(); app.Visible = false; app.UserControl = true; Workbooks wbs = app.Workbooks; Workbook wb = wbs.Open(path, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing); Sheets wss = wb.Worksheets; Worksheet ws = (Worksheet)wss.get_Item(1); Console.WriteLine(ws.Cells[2,1].Value); NAR(ws); ws = null; NAR(wss); wss = null; wb.Close(); NAR(wb); wb = null; wbs.Close(); NAR(wbs); wbs = null; app.Quit(); NAR(app); app = null; GC.Collect(); } private static void NAR(object o) { try { while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) ; } catch { } finally { o = null; } }结果能正常关闭Excel进程。
0 0
- C#中通过com组件操作excel不能关闭的问题
- 关于C#中,采用excel com组件操作excel的问题
- C# COM组件操作Excel
- PHP通过COM操作EXCEL,的问题
- C#使用Excel的COM组件操作Excel
- Asp.Net中Word,Excel等office com组件操作权限的问题
- Asp.Net中Word,Excel等office com组件操作权限的问题
- C#操作Excel时找不到“EXCEL9.OLB”com组件,可以通过如下方法实现:
- C#中通过COM读写Excel
- 在使用C#调用COM组件操作Excel的时候阻止其他程序对此文件操作
- 关于vc操作Excel表格后出现Excel.exe进程不能关闭的问题
- C#用Com组件解析Excel文件 然后删除Excel 并且彻底关闭Excel进程
- C#用Com组件解析Excel文件 然后删除Excel 并且彻底关闭Excel进程
- C#通过引用Office Excel (2007) 组件实现对Excel文件的操作
- C#通过引用Office Excel (2007) 组件实现对Excel文件的操作
- C#通过引用Office Excel (2007) 组件实现对Excel文件的操作
- C#通过引用Office Excel (2007) 组件实现对Excel文件的操作
- C#通过引用Office Excel (2007) 组件实现对Excel文件的操作
- 2014-7-16任务完成情况
- poj 1038 Bugs Integrated, Inc.(动态规划状态压缩)
- ctags的安装和使用
- [Leetcode] reverse words in a string 反转单词
- Animation动画效果的实现(四)------总结
- C#中通过com组件操作excel不能关闭的问题
- SSD的SMART属性
- 要做的事
- UINavigationController使用详解
- C语言比较 回车符和零的区别
- 软件工程
- POJ1680
- ACdream 1101 瑶瑶想要玩滑梯
- C语言实现 递归法 数字转换成字符串