读书人

帮小弟我看这段代码关于关闭excel进

发布时间: 2011-12-27 22:22:55 作者: rapoo

帮我看这段代码,关于关闭excel进程
Finally '无论如何关闭EXCEL
If Not oExcel Is Nothing Then
oExcel.Workbooks.Close()
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
End If
If Not oSheet Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
End If
oSheet = Nothing
oExcel = Nothing
GC.Collect()
End Try
在程序中打开调用excel,excel打开后需要关闭excel进程,但常出现的情况是调用一次excel,系统就多两个excel.exe的进程。程序是别人写的,关于excel调用我不是很熟,大家给分析下,为什么会出现打开好几个进程关不掉的情况呢?

[解决办法]
建议用单例模式,.net关闭Excel进程问题没有通用解决方案.
[解决办法]
关闭不掉,主要是oExcel.Quit()没有执行到,你可以跟踪下。把代码改成如下试试
If Not oExcel Is Nothing Then
Try
oExcel.Workbooks.Close()
Catch ex As Exception
End Try
oExcel.Quit()
End If
oSheet = Nothing
oExcel = Nothing
GC.Collect()

读书人网 >VB Dotnet

热点推荐