关闭vb.net应用后,Excel仍在运行 [英] Excel still running after close vb.net app

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

问题描述

我在关闭Excel文件时遇到了一些问题.我正在做一个程序,该程序打开一些Excel文件,使用该信息,然后关闭它们.
我尝试了一些不同的代码,但由于EXCEL进程仍在运行,因此无法正常工作.

I'm having some problems closing Excel files. I am doing a program that opens some Excel files, uses the information and then close them.
I tried some different codes, but they didn't work because the EXCEL process is still running.

我的第一个代码:

My first code:

Dim aplicacaoexcel As New Excel.Application
Dim livroexcel As Object
Dim folhaexcel As Excel.Worksheet

livroexcel = aplicacaoexcel.Workbooks.Open("C:\Users\LPO1BRG\Desktop\Software Fiabilidade\Tecnicos.xlsx", UpdateLinks:=False, ReadOnly:=False, Password:="qmm7", WriteResPassword:="qmm7")
folhaexcel = livroexcel.sheets("Folha1")

aplicacaoexcel.DisplayAlerts = False
aplicacaoexcel.Visible = False
folhaexcel = Nothing
livroexcel.Close()
livroexcel = Nothing
aplicacaoexcel.Quit()
aplicacaoexcel = Nothing

然后我添加了此内容:System.GC.Collect(),但它仍然没有关闭Excel进程.

Then I added this: System.GC.Collect() but it still not closing the Excel process.

现在我正在尝试:

Now I am trying this:

Dim process() As Process = system.Diagnostics.Process.GetProcessesByName("EXCEL")

For Each p As Process In process
   p.Kill()
Next

实际上,这是可行的,但是它会关闭所有Excel文件(甚至包括那些未由我的程序打开的Excel文件).

Actually, this one is working, but it closes all the Excel files (even those that are not opened by my program).

我该怎么做才能仅关闭程序打开的Excel文件? :)

What can I do to close just the Excel files opened by my program? :)

推荐答案

发布Excel.Application Interop COM对象比其他Office Interop对象要复杂一些,因为某些对象是在您不知情的情况下创建的,并且它们都必须是在实际关闭主应用程序之前发布.

Releasing the Excel.Application Interop COM object is a little bit trickier than other Office Interop objects, because some objects are created without your knowledge, and they all must be released before the main Application can be actually closed.

这些对象包括:

These objects include:

  • Excel.Application
  • Excel.Application.WorkBooks集合
  • WorkBooks集合打开了WorkBook
  • WorkBook Sheets集合
  • 工作表集合引用的工作表
  • The Excel.Application
  • The Excel.Application.WorkBooks collection
  • The WorkBooks collection opened WorkBook
  • The WorkBook Sheets collection
  • The Sheets collection referenced Worksheet

这些对象必须全部释放才能终止EXCEL进程.

These objects must all be released in order to terminate the EXCEL process.

一个简单的解决方案是对所有COM对象使用显式声明/赋值:

A simple solution is to use explicit declarations/assignment for all the COM objects:

Dim ExcelApplication As New Microsoft.Office.Interop.Excel.Application()
Dim ExcelWorkbooks As Workbooks = ExcelApplication.Workbooks
Dim MyWorkbook As Workbook = ExcelWorkbooks.Open("[WorkBookPath]", False)
Dim worksheets As Sheets = MyWorkbook.Worksheets
Dim MyWorksheet As Worksheet = CType(worksheets("Sheet1"), Worksheet)

完成后,将它们全部释放:

When you're done, release them all:

Imports System.Runtime.InteropServices

Marshal.ReleaseComObject(MyWorksheet)
Marshal.ReleaseComObject(worksheets)

MyWorkbook.Close(False)   '<= False if you don't want to save it!
Marshal.ReleaseComObject(MyWorkbook)

ExcelWorkbooks.Close()
Marshal.ReleaseComObject(ExcelWorkbooks)

ExcelApplication.Quit()
Marshal.FinalReleaseComObject(ExcelApplication)

Marshal.CleanupUnusedObjectsInCurrentContext()

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

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