首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

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

2011-12-27 
帮我看这段代码,关于关闭excel进程Finally无论如何关闭EXCELIfNotoExcelIsNothingThenoExcel.Workbooks.Cl

帮我看这段代码,关于关闭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()

热点排行