Excel进程在VB.net关闭后仍然运行 [英] Excel process still runs after closing in VB.net

查看:273
本文介绍了Excel进程在VB.net关闭后仍然运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基本上是如何结束使用excel时运行的Excel.exe进程。在应用程序中,我打开并使用一张excel工作簿和几张表,然后让他们让用户按照他们的要求做,我的问题是我的应用程序永远不会放弃Excel过程。



如果应用程序在关闭excel之前关闭,那么当excel关闭时,该进程结束,否则如果在关闭excel之后关闭我的应用程序,该进程将保持运行。



我已经尝试过一些我在互联网上发现的一些事情,用GC.collect和等待终结者等等,但是这两个方面都不行。



<我也可以快乐地关闭excel进程,唯一的问题是不知道我是否关闭用户重要的电子表格,他们忽略了保存还是我的。



我不知道我的代码是否有帮助的答案,但我正在使用microsoft.office.interop.excel获得excel,我正在使用一个已经保存在应用程序的资源文件夹中的工作簿。



-Edit -



这是我尝试过的一切,我知道这有点过分,但遗憾的是,它仍然没有结束流程

  Marshal.ReleaseComObject(FirstWorksheet)
Marshal.FinalReleaseComObject(FirstWorksheet)
Marshal.ReleaseComObject(SecondWorksheet)
元帅
Marshal.ReleaseComObject(ThirdWorksheet)
Marshal.FinalReleaseComObject(ThirdWorksheet)
Marshal.ReleaseComObject(FourthWorkSheet)
Marshal.FinalReleaseComObject(FourthWorkSheet)
元帅.ReleaseComObject(xlRange)
Marshal.FinalReleaseComObject(xlRange)
Marshal.ReleaseComObject(SecondxlRange)
Marshal.FinalReleaseComObject(SecondxlRange)
Marshal.ReleaseComObject(thirdxlRange)
元帅.FinalReleaseComObject(thirdxlRange)
Marshal.ReleaseComObject(4thxlRange)
Marshal.FinalReleaseComObject(4thxlRange)
Marshal.ReleaseComObject(.activeworkbook)
Marshal.FinalReleaseComObje ct(.activeworkbook)
Marshal.ReleaseComObject(excelApplication)
Marshal.FinalReleaseComObject(excelApplication)
MSExcelControl.QuitExcel()'与其他人一起工作的一个功能是为了关闭excel的进程
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

-Edit-这是quitExcel方法

 朋友共享子QuitExcel()
如果不是getExcelProcessID = -1然后
如果不是excelApp没有,然后
'关闭并退出
使用excelApp
尝试
直到。 Workbooks.Count = 0
'关闭所有打开的文档,而不保存
.Workbooks(1).Close(SaveChanges:= 0)
循环
捕获exExcel作为异常
'不做任何
结束尝试
尝试
.ActiveWorkbook.Close(SaveChanges:= 0)
Catch ex As Exception
'不执行任何
结束尝试

尝试
。 Quit()
Catch ex As Exception
'do nothing
最后
myExcelProcessID = -1
结束尝试
结束
excelApp = Nothing
如果
结束If
End Sub

他已经尝试与他的课程中的进程ID做同样的事情,问题是他没有工作,这就是为什么我尝试再次获得自己的进程ID,这是工作

解决方案

好的解决方案Alex,我们不应该这样做,但是我们这样做,EXCEL就不会结束。我采取了你的解决方案并创建了下面的代码,我的ExcelProcessInit之前我的应用程序导入或Excel导出,然后调用ExcelProcessKill完成后。

 私有mExcelProcesses()作为进程

私有子ExcelProcessInit()
尝试
'获取所有当前正在运行的Excel应用程序的进程ID
mExcelProcesses = Process.GetProcessesByName (Excel)
Catch ex As Exception
End尝试
End Sub

私有子ExcelProcessKill()
Dim oProcesses()As Process
Dim bFound As Boolean

尝试
'获取当前正在运行的Excel应用程序的进程ID
oProcesses = Process.GetProcessesByName(Excel)

如果oProcesses.Length> 0然后
对于i As Integer = 0 To oProcesses.Length - 1
bFound = False

对于j As Integer = 0到mExcelProcesses.Length - 1
如果oProcesses(i).Id = mExcelProcesses(j).Id然后
bFound = True
退出
结束如果
下一个

如果不是bFound
oProcesses(i).Kill()
End If
Next
End If
Catch ex As Exception
End尝试
End Sub

Private Sub MyFunction()
ExcelProcessInit()
ExportExcelData()'无论您为此编写的代码...
ExcelProcesKill()
End Sub


My question is basically just how to end the Excel.exe process that runs when using excel. In the application I open and use an excel workbook with a couple sheets, then leave them for the user to do as they please, my problem is that my application never lets go of the Excel process.

If the application is closed before closing excel, the process ends when excel is closed, otherwise if I close my application after closing excel, the process stays running.

I've tried a few things I've found around the internet to do with GC.collect and waiting for pending finalizers or something along those lines however neither worked.

I can also close the excel process happily, the only issue is not knowing whether I'm closing a users important spreadsheet that they have neglected to save yet or mine.

I'm not sure if any of my code will help with an answer but I'm using microsoft.office.interop.excel to get to excel, and I am using a workbook already saved in the application's resources folder.

-Edit-

Here's everything I've tried, I know it's a little overkill but sadly it still doesn't end the process

Marshal.ReleaseComObject(FirstWorksheet)
Marshal.FinalReleaseComObject(FirstWorksheet)
Marshal.ReleaseComObject(SecondWorksheet)
Marshal.FinalReleaseComObject(SecondWorksheet)
Marshal.ReleaseComObject(ThirdWorksheet)
Marshal.FinalReleaseComObject(ThirdWorksheet)
Marshal.ReleaseComObject(FourthWorkSheet)
Marshal.FinalReleaseComObject(FourthWorkSheet)
Marshal.ReleaseComObject(xlRange)
Marshal.FinalReleaseComObject(xlRange)
Marshal.ReleaseComObject(SecondxlRange)
Marshal.FinalReleaseComObject(SecondxlRange)
Marshal.ReleaseComObject(thirdxlRange)
Marshal.FinalReleaseComObject(thirdxlRange)
Marshal.ReleaseComObject(fourthxlRange)
Marshal.FinalReleaseComObject(fourthxlRange)
Marshal.ReleaseComObject(.activeworkbook)
Marshal.FinalReleaseComObject(.activeworkbook)
Marshal.ReleaseComObject(excelApplication)
Marshal.FinalReleaseComObject(excelApplication)
MSExcelControl.QuitExcel() 'A function made by someone else I work with that was meant to close excel's process
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

-Edit- Here's the quitExcel method

Friend Shared Sub QuitExcel()
    If Not getExcelProcessID = -1 Then
        If Not excelApp Is Nothing Then
            'Close and quit
            With excelApp
                Try
                    Do Until .Workbooks.Count = 0
                        'Close all open documents without saving
                        .Workbooks(1).Close(SaveChanges:=0)
                    Loop
                Catch exExcel As Exception
                    'Do nothing
                End Try
                Try
                    .ActiveWorkbook.Close(SaveChanges:=0)
                Catch ex As Exception
                    'Do nothing
                End Try

                Try
                    .Quit()
                Catch ex As Exception
                    'Do nothing
                Finally
                    myExcelProcessID = -1
                End Try
            End With
            excelApp = Nothing
        End If
    End If
End Sub

He had already tried to do the same thing with the process ID's in his class, the problem was his wasn't working which is why I tried to get the process ID again myself which has worked

解决方案

Good solution Alex, we shouldn't have to do this, but we do, EXCEL just won't end. I took your solution and created the code below, I call ExcelProcessInit before my app imports or exports with Excel, then call ExcelProcessKill after it's complete.

Private mExcelProcesses() As Process

Private Sub ExcelProcessInit()
  Try
    'Get all currently running process Ids for Excel applications
    mExcelProcesses = Process.GetProcessesByName("Excel")
  Catch ex As Exception
  End Try
End Sub

Private Sub ExcelProcessKill()
  Dim oProcesses() As Process
  Dim bFound As Boolean

  Try
    'Get all currently running process Ids for Excel applications
    oProcesses = Process.GetProcessesByName("Excel")

    If oProcesses.Length > 0 Then
      For i As Integer = 0 To oProcesses.Length - 1
        bFound = False

        For j As Integer = 0 To mExcelProcesses.Length - 1
          If oProcesses(i).Id = mExcelProcesses(j).Id Then
            bFound = True
            Exit For
          End If
        Next

        If Not bFound Then
          oProcesses(i).Kill()
        End If
      Next
    End If
  Catch ex As Exception
  End Try
End Sub

Private Sub MyFunction()
  ExcelProcessInit()
  ExportExcelData() 'Whatever code you write for this...
  ExcelProcesKill()
End Sub

这篇关于Excel进程在VB.net关闭后仍然运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆