Excel应用程序不退出

嗨检查我的代码我自动上传从Excel中的VB.NET这里是我的代码

导入语句

Imports Excel = Microsoft.Office.Interop.Excel 

releaseObject函数

 Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub 

button点击事件

 Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonUpload.Click If IsPostBack Then Dim xlApp As Excel.Application Dim xlWorkBooks As Excel.Workbooks Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim range As Excel.Range connStr = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString conn = New SqlConnection(connStr) dat = System.DateTime.Now Filepath = Path.GetFullPath(fileUploadBOM.PostedFile.FileName) sFileName = Path.GetFileName(fileUploadBOM.PostedFile.FileName) FileFormat = Path.GetExtension(Filepath) v_bom_type = "IMPORT" If FileFormat.Equals(".xls") Or FileFormat.Equals(".xlsx") Then System.IO.File.Delete("C:\inetpub\wwwroot\Uploads\" & sFileName) fileUploadBOM.PostedFile.SaveAs(sFileDir + sFileName) Try xlApp = New Excel.ApplicationClass xlWorkBooks = xlApp.Workbooks xlWorkBook = xlWorkBooks.Open(sFileDir + sFileName) xlWorkSheet = xlWorkBook.Worksheets("BOM for Import") range = xlWorkSheet.Cells Catch ex As Exception releaseObject(xlApp) 'GetWindowThreadProcessId(xlApp.Hwnd, processID) 'release(processID) End Try 

一些如果条件喜欢..

  Dim R As String R = CType(range.Cells(4, 2), Excel.Range).Value() If Not R Is Nothing Then If (R.Trim = "") Then Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('please enter the OEM for logistics cost'); </script>") releaseObject(range) releaseObject(xlWorkSheet) xlWorkBook.Save() xlWorkBook.Close() releaseObject(xlWorkBook) xlWorkBooks.Close() releaseObject(xlWorkBooks) xlApp.Quit() releaseObject(xlApp) 'GetWindowThreadProcessId(xlApp.Hwnd, processID) 'MsgBox(processID) 'release(processID) Exit Sub Else v_logiccost = CType(range.Cells(4, 2), Excel.Range).Value() End If Else Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('please enter the OEM for logistics cost'); </script>") releaseObject(range) releaseObject(xlWorkSheet) xlWorkBook.Save() xlWorkBook.Close() releaseObject(xlWorkBook) xlWorkBooks.Close() releaseObject(xlWorkBooks) xlApp.Quit() releaseObject(xlApp) 'GetWindowThreadProcessId(xlApp.Hwnd, processID) 'MsgBox(processID) 'release(processID) Exit Sub End If '' No of yrs of support Dim P As String P = CType(range.Cells(6, 2), Excel.Range).Value() releaseObject(range) If Not P Is Nothing Then If (IsNumeric(P) = True) Then v_year = P Else Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('No Of Years Support Should Be A Number.'); </script>") releaseObject(range) releaseObject(xlWorkSheet) xlWorkBook.Save() xlWorkBook.Close() releaseObject(xlWorkBook) xlWorkBooks.Close() releaseObject(xlWorkBooks) xlApp.Quit() releaseObject(xlApp) 'GetWindowThreadProcessId(xlApp.Hwnd, processID) 'MsgBox(processID) 'release(processID) Exit Sub End If End If 

函数调用释放到最后

 releaseObject(range) releaseObject(xlWorkSheet) xlWorkBook.Save() xlWorkBook.Close() releaseObject(xlWorkBook) xlWorkBooks.Close() releaseObject(xlWorkBooks) xlApp.Quit() releaseObject(xlApp) 

这是我的整个代码我猜问题是与Range.cells参考RCWbuild议我在哪里做错了

一些事情

A)在编码方面,我始终坚信1个入口点和1个退出点。 在你的代码中你有几个退出点。 你的代码现在看起来像这样

  If A = B Then ' '~~> Rest of the code ' 'release object Exit Sub Else End If If C = D Then Else ' '~~> Rest of the code ' 'release object Exit Sub End If releaseObject(xlrange) releaseObject(xlWorkSheet) releaseObject(xlWorkBook) releaseObject(xlApp) 

如果我们正确地closures我们的对象,跟踪变得非常困难。 最好的方法是将它们合并,然后在离开子工具之前释放对象。 您的上面的代码可以重写为

  If A = B Then ' '~~> Rest of the code ' Else If C = D Then Else ' '~~> Rest of the code ' End If End If releaseObject(xlrange) releaseObject(xlWorkSheet) releaseObject(xlWorkBook) releaseObject(xlApp) 

B)正如亚历克斯和克里斯提到的,以正确的顺序释放你的对象(你到底在做什么,而不是在代码的中间)。 喜欢这个

  releaseObject(xlrange) releaseObject(xlWorkSheet) releaseObject(xlWorkBook) releaseObject(xlApp) 

如果你join了我的第一个build议,那么你不必在任何地方维护发布对象代码,而只是到最后。

C)我看到你已经宣布你的Excel对象为PUBLIC除了ButtonUpload_Click之外,你是否正在使用它们

  1. 如果是的话,例如Form Load ,那么确保你正确地释放它们
  2. 如果不是,则将其移动到ButtonUpload_Click

如果你包含上述build议,那么在使用TWO DOT规则时我不会看到释放对象的问题。

rest我没有看到你现有的代码有任何问题。 它对我来说工作得很好。

感谢所有解决

 Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) Catch ex As Exception Finally obj = Nothing End Try End Sub