任务管理器中的Excel实例(excel.exe)就不会被杀死。
Private Sub Extract_Click(sender As Object, e As EventArgs) Handles Extract.Click
Dim xlApp As New Excel.Application
Dim wbXLsource As Excel.Workbook
Dim wbXLtarget As Excel.Workbook
Dim shXLsource As Excel.Worksheet
Dim shXLtarget As Excel.Worksheet
Dim datestart As Date = Date.Now
wbXLsource = xlApp.Workbooks.Open(xlSourcePath)
wbXLtarget = xlApp.Workbooks.Open(xlTargetPath)
Dim dateEnd As Date = Date.Now
End_Excel_App(datestart, dateEnd)
End Sub
Private Sub End_Excel_App(datestart As Date, dateEnd As Date)
Dim xlp() As Process = Process.GetProcessesByName("EXCEL")
For Each Process As Process In xlp
If Process.StartTime >= datestart And Process.StartTime <= dateEnd Then
Process.Kill()
Exit For
End If
Next
End Sub
我尝试使用垃圾收集器概念,但excel实例只有在关闭应用程序后才会被杀死。我需要在按钮执行结束后关闭excel实例(excel.exe)
答案 0 :(得分:0)
通过调用xlApp.Quit()
关闭应用程序会将其关闭,但不会从内存中删除。
要释放记忆,您可以按照Siddharth Rout's excellent answer提供的建议:
Private Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Loop While intRel > 0
MsgBox("Final Released obj # " & intRel)
Catch ex As Exception
MsgBox("Error releasing object" & ex.ToString)
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
答案 1 :(得分:0)
使用此代码....我一直在使用它,每次都关闭
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
If proc.MainWindowTitle.Trim() = "" Then
proc.Kill()
End If
Next
如果你有一个Try Catch错误处理程序......
Try
'stuff more stuff
Catch Ex as Exception
For Each Proc in System.......
......
Next
Finally
GC.Collect()
End Try
每次都有效。