如何结束excel.exe进程?

我试图从vb.net 3.5获得Excel文件的excel文件名,但它打开但excel.exe仍然在这个过程中。如何在不从任务管理器中删除excel.exe的情况下停止进程? 我意识到新的excel.application开始了新的过程。 我试图使用戒烟,关闭和处置.......................没有任何效果 以下是我的代码
   Dim sheetName As New Excel.XlSheetType
   Dim newExcell As New Excel.Application
   Dim newWorkBook As Excel.Workbook = app.Workbooks.Open(MyFileName)
   Dim worksheetName As String
   Dim ws As Excel.Worksheet = CType(WB.Worksheets.Item(1), Excel.Worksheet)
    worksheetName = ws.Name
我不能使用kill因为有其他excel应用程序在运行,所以如何从处理器关闭这个特定的excel.exe。请帮忙     
已邀请:
这篇KB文章描述了这个问题。 但是,要解决这个问题并不容易,因为您必须确保在实例化的每个Excel对象上调用Marshal.ReleaseComObject - 而且很容易错过一个。 例如,以下代码隐式引用Workbooks对象:
Dim newWorkBook As Excel.Workbook = app.Workbooks.Open(MyFileName) 
要释放Workbooks对象,您需要显式引用它,以便您有一个可以调用Marshal.ReleaseComObject的引用:
Dim objWorkbooks As Excel.Workbooks = app.Workbooks
Dim newWorkbook As Excel.Workbook = objWorkbooks.Open(MyFileName)
...
Marshal.ReleaseComObject(objWorkbooks)
Marshal.ReleaseComObject(newWorkbook)
...
您还应该使用Try / Finally来确保即使抛出异常也会调用ReleaseComObject。     
很多人不建议杀死这个过程;看到 如何正确清理Excel互操作对象 并了解.net中的垃圾收集 编辑:另一方面,许多人不建议使用GC.Collect。请参阅使用GC.Collect()有什么错误? 根据我的经验,杀死这个过程是最快最简单的。此代码仅杀死它启动的确切进程,而不是其他进程。 确保关闭所有打开的工作簿,退出应用程序,释放xlApp对象。最后检查过程是否仍然存在,如果是,那么杀死它。 这是我使用的代码:(每次都有效)
Sub UsingExcel()

    'declare process; will be used later to attach the Excel process
    Dim XLProc As Process

    'call the sub that will do some work with Excel
    'calling Excel in a separate routine will ensure that it is 
    'out of scope when calling GC.Collect
    'this works better especially in debug mode
    DoOfficeWork(XLProc)

    'Do garbage collection to release the COM pointers
    'http://support.microsoft.com/kb/317109
    GC.Collect()
    GC.WaitForPendingFinalizers()

    'I prefer to have two parachutes when dealing with the Excel process
    'this is the last answer if garbage collection were to fail
    If Not XLProc Is Nothing AndAlso Not XLProc.HasExited Then
        XLProc.Kill()
    End If

End Sub

'http://msdn.microsoft.com/en-us/library/ms633522%28v=vs.85%29.aspx
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, _
    ByRef lpdwProcessId As Integer) As Integer
End Function

Private Sub ExcelWork(ByRef XLProc As Process)

    'start the application using late binding
    Dim xlApp As Object = CreateObject("Excel.Application")

    'or use early binding
    'Dim xlApp As Microsoft.Office.Interop.Excel

    'get the window handle
    Dim xlHWND As Integer = xlApp.hwnd

    'this will have the process ID after call to GetWindowThreadProcessId
    Dim ProcIdXL As Integer = 0

    'get the process ID
    GetWindowThreadProcessId(xlHWND, ProcIdXL)

    'get the process
    XLProc = Process.GetProcessById(ProcIdXL)


    'do some work with Excel here using xlApp

    'be sure to save and close all workbooks when done

    'release all objects used (except xlApp) using NAR(x)


    'Quit Excel 
    xlApp.quit()

    'Release
    NAR(xlApp)

End Sub

Private Sub NAR(ByVal o As Object)
    'http://support.microsoft.com/kb/317109
    Try
        While (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
        End While
    Catch
    Finally
        o = Nothing
    End Try
End Sub
    

bab

使用excel应用程序对象。拨打
Quit()
像例如:
Dim app As New Excel.Application    
Try
    '...
    'After doing your stuff
    '...
    app.Quit()
Catch ex As Exception
    'Log your exception
    System.Diagnostics.Print("Error: " + ex.Message)
End Try
    

要回复问题请先登录注册