杀死特定的Excel流程

时间:2019-01-31 11:31:42

标签: asp.net vb.net excel-2010

我正在用VB.Net创建一个Excel报告。该报告创建成功,最后我取消了进程。但是,当同一份报告同时在不同的计算机上运行时,即由多个用户使用,它将杀死所有进程,并且只有一个用户能够创建该报告。

您能否建议我,一旦创建了特定的excel流程,我该如何关闭它。以下是我的代码

If Not _xlApp.Workbooks Is Nothing Then
        For Each wb In _xlApp.Workbooks
            wb.Close(False)
        Next
        _xlApp.Workbooks.Close()
    End If
    _xlApp.DisplayAlerts = False
    _xlApp.Quit()

    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.Collect()
    GC.WaitForPendingFinalizers()


    If _oSheet IsNot Nothing Then
        Runtime.InteropServices.Marshal.ReleaseComObject(_oSheet)
    End If



    Runtime.InteropServices.Marshal.ReleaseComObject(_oBook)

    _xlApp.Quit()
    Runtime.InteropServices.Marshal.ReleaseComObject(_xlApp)


    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.Collect()
    GC.WaitForPendingFinalizers()
    Dim proc As Diagnostics.Process

    For Each proc In Diagnostics.Process.GetProcessesByName("EXCEL")
        Try
            proc.Kill()
        Catch ex As Exception

        End Try

    Next

问题是创建Excel时无法识别应该关闭的进程的PID。

1 个答案:

答案 0 :(得分:0)

您不需要像这样手动终止进程。确实需要一段时间才能使垃圾回收获得COM对象。在我退出表单和大多数应用程序之前,我的趋向于不删除对象。这里有一些链接可能会有所帮助。

Release Excel COM Objects

Why doesn't Excel Quit

Understanding Garbage Collection in .NET

相关问题