如何结束excel.exe进程?

即时通讯试图从Excel vb.net 3.5 excel文件的excel表名称,但它打开,但excel.exe仍然在这个过程中。 如何在不从任务pipe理器中删除excel.exe的情况下停止进程?

我意识到新的excel.application开始新的过程。

我试图使用退出,closures和处置…………………..没有任何工作

以下是我的代码

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 

我不能使用杀死,因为有其他的Excel应用程序正在运行,所以如何从处理器closures这个特定的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) ... 

即使引发exception,您也应该使用Try / Finally确保ReleaseComObject被调用。

许多人不build议杀死这个过程; 请参阅如何正确清理Excel互操作对象和了解.net中的垃圾收集

编辑:另一方面,很多人不推荐使用GC.Collect。 看看使用GC.Collect()有什么错误?

以我的经验来说,杀死这个过程是最快最简单的。 这段代码只会杀死它启动的确切进程,没有别的。

确保closures所有打开的工作簿,退出应用程序,释放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 

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