【问题标题】:Kill a specific Excel process杀死特定的 Excel 进程
【发布时间】:2019-01-31 11:31:41
【问题描述】:

我正在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。

【问题讨论】:

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


    【解决方案1】:

    您不需要像那样手动终止进程。垃圾收集确实需要一段时间才能获得 COM 对象。在我退出表单和大部分应用程序之前,我的往往不会删除对象。以下是一些可能会有所帮助的链接。

    Release Excel COM Objects

    Why doesn't Excel Quit

    Understanding Garbage Collection in .NET

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多