VB.NET 使用Microsoft.Office.Interop进行EXCEL操作时彻底关闭EXCEL进程

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Public Class 关闭EXCEL
    '
    ' TODO: 在此处添加构造函数逻辑
    '
    Public Sub New()
    End Sub
     _
    Public Shared Function GetWindowThreadProcessId(hwnd As IntPtr, ByRef ID As Integer) As Integer
    End Function
    Public Shared Sub Kill(excel As Excel.Application)
        Dim t As New IntPtr(excel.Hwnd)
        '得到这个句柄,具体作用是得到这块内存入口 
        Dim k As Integer = 0
        GetWindowThreadProcessId(t, k)
        '得到本进程唯一标志k
        Dim p As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(k)
        '得到对进程k的引用
        p.Kill()
        '关闭进程k
    End Sub
    Public Shared Sub 关闭EXCEL进程(excelApp As Excel.Application)
        excelApp.Quit()
        Kill(excelApp)
        excelApp = Nothing
        GC.Collect()
    End Sub
End Class

 

你可能感兴趣的:(vb.net,彻底关闭EXCEL进程)