如何结束excel.exe进程?

时间:2011-03-04 03:38:48

标签: .net vb.net excel .net-3.5

我试图从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。请帮忙

3 个答案:

答案 0 :(得分:5)

This KB article描述了这个问题。

然而,要解决这个问题并不容易,因为你必须确保在实例化的每个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。

答案 1 :(得分:2)

许多人不建议杀死这个过程;看到 How to properly clean up Excel interop objectsUnderstanding Garbage Collection in .net

编辑:另一方面,许多人不建议使用GC.Collect。见What's so wrong about using 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

答案 2 :(得分:0)

使用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