无法从VB.NET应用程序关闭Excel进程 [英] Unable to close Excel process from VB.NET app

查看:450
本文介绍了无法从VB.NET应用程序关闭Excel进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的类以打开并在excel文件中收集工作表的名称。它打开文件并返回每个工作表的名称。不幸的是,我无法关闭该文件。这将使Excel.exe进程挂起。即使我尝试在本课底部关闭它,这个过程仍然挂起来。我必须手动转到Windows任务管理器,并杀死该进程,然后再次使用该文件。即使我退出应用程序,它仍然在那里。关于如何从代码中杀死这个进程的任何建议?

I created the class below to open and gather the names of worksheets in an excel file. It does as it should in opening the file and returning the names of the each individual worksheet. Unfortunatly, I'm unable to close the file. This keeps the Excel.exe process hanging. Even after I attempt to close it at the bottom of this class the process still hangs. I have to manually goto Windows Task Manager and kill the process before I can use the file again. Even if I exit out of the application it still is out there. Any suggestion on how to kill this process from the code?

Imports Excel = Microsoft.Office.Interop.Excel

Public Class ExcelWrapper

Dim strTypeSelection As String = String.Empty
Dim strFilePath As String = String.Empty

Function GetWorksheetsByName(ByVal strfilePath As String) As Array

    Dim xlsApp As Excel.Application
    Dim xlsWorkBook As Excel.Workbook

    xlsApp = New Excel.Application
    xlsWorkBook = xlsApp.Workbooks.Open(strfilePath)
    Dim intWsCount As Integer = xlsApp.Worksheets.Count

    Dim sarWorksheetName(intWsCount - 1) As String

    Dim i As Integer = 0
    'gathers the names off all the worksheets 
    For Each totalWorkSheets In xlsApp.Worksheets
        sarWorksheetName(i) = totalWorkSheets.Name
        i += 1
    Next totalWorkSheets

    xlsWorkBook.Close(strfilePath)
    xlsApp.DisplayAlerts = False
    xlsApp.Quit()

    Return sarWorksheetName
End Function

End Class


推荐答案

我也遇到过这个问题,我记得解决方案是调用 Marshal.ReleaseComObject(Object obj) Marshal.FinalReleaseComObject(Object obj)来自Office库的所有对象。我可以很容易地错了。祝你好运,办公自动化是一个巨大的痛苦。

I've run into this issue before as well, and as I recall the solution was to call Marshal.ReleaseComObject(Object obj) or Marshal.FinalReleaseComObject(Object obj) on all of the objects that the come from the Office libraries. I could very easily be wrong though. Good luck, Office automation is a huge pain.

这篇关于无法从VB.NET应用程序关闭Excel进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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