在vb.net中关闭excel应用程序后浮动Excel.exe

时间:2016-03-18 20:33:39

标签: vb.net excel

即使在关闭工作簿并退出excel应用程序之后,我也遇到了浮动excel.exe文件的问题。关闭后,我发布工作表,工作簿和应用程序对象。我错过了要释放的对象吗?

下面的代码打开我的excel文件,并将特定单元格与窗口形式组合框中的用户所选项目进行比较。

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbTest.SelectedIndexChanged
    FolderBrowserDialog1.SelectedPath = "K:\ETL Test Files\" & Main2.portalInst.txtYear.Text & "\" & Main2.portalInst.txtCompany.Text & "\" & Main2.portalInst.txtReport.Text & "\Test Data\Test Results-1.xlsx"
    Dim rows As Integer = 2
    Dim excel_app As New Microsoft.Office.Interop.Excel.Application
    excel_app.Visible = True
    Dim workbook As excel.Workbook = excel_app.Workbooks.Open(FolderBrowserDialog1.SelectedPath) 'each test data folder will have this file
    Dim worksheet As excel.Worksheet = workbook.Worksheets("Sheet1")
    While (worksheet.Cells.Range("A" & rows).Value IsNot Nothing)
        rows = rows + 1
    End While
    If cmbTest.SelectedIndex <> -1 Then
        txtCategory.Enabled = True
        Button2.Enabled = True
        Button1.Enabled = True
        grpComp.Enabled = True
        grpSuscep.Enabled = True
        For i As Integer = 2 To rows Step 1
            If worksheet.Cells.Range("A" & i).Value IsNot Nothing Then
                If worksheet.Cells.Range("A" & i).Value.ToString & "-" & worksheet.Cells.Range("D" & i).Value.ToString = cmbTest.SelectedItem.ToString Then
                    txtCategory.Text = worksheet.Cells.Range("D" & i).Value.ToString
                End If
            End If
        Next
    End If
    workbook.Close()
    excel_app.Quit()
    ReleaseObject(worksheet)
    ReleaseObject(workbook)
    ReleaseObject(excel_app)
End Sub

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        Dim intRel As Integer = 0
        Do
            intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        Loop While intRel > 0
        MsgBox("Final Released obj # " & intRel)
    Catch ex As Exception
        MsgBox("Error releasing object" & ex.ToString)
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

感谢任何帮助。 提前谢谢!

-Rueben Ramirez

0 个答案:

没有答案