如何正确清理excel com对象。当使用对象
时,在任务管理器中悬挂Excel进程在任务管理器中运行如果没有错误,则退出应用程序后没有excel.exe进程在taskmanager
中留下我的示例代码。如果您评论错误行并关闭应用程序,则一切都很好。如何处理诸如此类错误的错误案件中的清理?
Imports Microsoft.Office.Interop
Public Class Form1
Private xlApp As Excel.Application
Private xlWorkBook As Excel.Workbook
Private misValue As Object
Property xlWorkSheet As Excel.Worksheet
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
''EXCEL CREATION/INITAILIAZATION
misValue = System.Reflection.Missing.Value
xlApp = New Microsoft.Office.Interop.Excel.Application()
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
xlApp = Nothing
End If
xlWorkBook = xlApp.Workbooks.Add(misValue)
''WRITE TO WORKSHEET
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "THIS"
xlWorkSheet.Cells(1, 2) = "IS"
xlWorkSheet.Cells(1, 3) = "A"
xlWorkSheet.Cells(1, 4) = "TEST"
''FORCEFULLY CAUSING ERROR, NOW THE EXCEL PROCESS HANGING IN TASK MANAGER
xlWorkSheet.Cells(1, -1) = "ERROR LINE"
''SAVE WORKSHEET
Dim Name = DateTime.Now.ToString("s").Replace(":", "_")
Dim Dir = AppDomain.CurrentDomain.BaseDirectory & Name & "Output.xls"
xlApp.DisplayAlerts = False
xlWorkBook.CheckCompatibility = False
xlWorkBook.DoNotPromptForConvert = True
xlWorkBook.SaveAs(Dir, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
Catch ex As Exception
Dim exMsg = ex.Message
Finally
''TIME TO CLEAN EXCEL STUFF
Cleanup()
End Try
End Sub
Private Sub Cleanup()
ReleaseObject(xlWorkSheet)
ReleaseObject(xlWorkBook)
xlApp.Quit()
ReleaseObject(xlApp)
End Sub
Private Sub ReleaseObject(ByRef obj As Object)
Try
If Not IsNothing(obj) And System.Runtime.InteropServices.Marshal.IsComObject(obj) Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
End If
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
弄清楚了。xlworkbook.close和xlapplication.quit在释放对象之前需要调用。这是更新的清理功能
Private Sub Cleanup()
xlWorkBook.Close(False)
xlApp.Quit()
ReleaseObject(xlWorkSheet)
ReleaseObject(xlWorkBook)
ReleaseObject(xlApp)
End Sub