从VB.NET中的内存中杀死EXCEL对象

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the kill process portion with a msgbox stating that excel was detected and it should be closed before proceeding as a precaution statement.

好吧,我们所有人都已经看到,即使调用.close,.dispose方法,任务管理器中的多个EXCEL.EXE也不会消失。 尝试使用此方法来杀死内存中的任何数据。 您可以复制kill函数来创建检查函数,并用msgbox替换kill进程部分,以表明已检测到excel,并应先关闭它,然后再进行预防措施声明。

I've seen many examples and discussions on how to properly exit and dispose of excel objects. Believe me, I tried them all. Some examples worked once, while others had no effect. I needed something to consistently dispose of the excel.exe trail in memory.

我已经看到了许多有关如何正确退出和处置excel对象的示例和讨论。 相信我,我尝试了所有。 一些示例只能工作一次,而其他则没有效果。 我需要一些东西来始终处理内存中的excel.exe。

Since the ultimate effect of killing the process is not desirable, I did create a IsExcelOpen() function, which is basically the same function of the CloseAllResidentExcels function except the prc.kill() process is eliminated and instead displays a warning message to the end user to please save and close all currently open spreadsheets before continuing. I was fully prepared for the hiss and moaning to ensue shortly after deploying the update — but surprisingly everyone had no issue with it.  

由于不需要终止进程的最终效果,因此我确实创建了一个IsExcelOpen()函数,该函数与CloseAllResidentExcels函数基本相同,只是取消了prc.kill()进程,而是在最后显示警告消息。用户请先保存并关闭所有当前打开的电子表格,然后再继续。 部署此更新后不久,我就为嘶嘶声和抱怨声做好了充分的准备-但令人惊讶的是,每个人对此都没有问题。

Many of the examples for closing an excel spreadsheet probably do work in the simple most case scenarios. But now I'm starting to wonder if the datagridview1 object has to be closed as well before quitting the excel application for these scenarios to work. I haven't had to much to time to find out why it's not closing properly. Everyone probably has a different version of Microsoft Office, different operating systems, and different framework versions that might have a factor in the hit and miss cases of what works and what doesn't.

在大多数情况下,关闭Excel电子表格的许多示例都可以使用。 但是现在我开始想知道在退出excel应用程序以使这些方案正常工作之前是否也必须关闭

Another possibility might be the way excel gets initialized in the code. Are we using duplicate references? Is the com interop enabled and initialized correctly. I tried various methods and none were able to get rid of the excel.exe objects in task manager.

另一种可能是excel在代码中初始化的方式。 我们是否使用重复引用? com互操作是否已启用并正确初始化。 我尝试了各种方法,但都无法摆脱任务管理器中的excel.exe对象。

Imports System 
Imports System.Diagnostics 
 
Private Sub printdocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles printdocument1.EndPrint 
 
    Do While True 
        'kill all resident excels in memory with extreme prejudice 
        If CloseAllResidentExcels() = True Then 
            Exit Do 
        End If 
    Loop 
     
    'reinit your vars here 
    xlApp = CType(CreateObject("Excel.Application"), Excel.Application) 
    wbkToShow = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet) 
    wshToShow = DirectCast(wbkToShow.Worksheets("Sheet1"), Excel.Worksheet) 
End Sub 
 
Private Function CloseAllResidentExcels() As Boolean 
 
    CloseAllResidentExcels = False 
 
    Try 
        'loop this process until there are no valid id's to return 
        Dim Prc As Process 
        Dim localByName As Process() = Process.GetProcessesByName("EXCEL") 
        Prc = Process.GetProcessById(localByName(0).Id) 
        Prc.Kill() 
 
    Catch ex As Exception 
        CloseAllResidentExcels = True 
    End Try 
 
End Function

翻译自: https://www.experts-exchange.com/articles/3260/Killing-EXCEL-objects-from-memory-in-VB-NET.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值