我创建了下面的类来打开并收集excel文件中工作表的名称.它在打开文件并返回每个单独工作表的名称时应该这样做.不幸的是,我无法关闭文件.这使Excel.exe进程保持挂起状态.即使我试图在
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我之前也遇到过这个问题,并且我记得解决方案是在来自Office库的所有对象上调用Marshal.ReleaseComObject(Object obj)或Marshal.FinalReleaseComObject(Object obj).我很容易就错了.祝你好运,办公自动化是一个巨大的痛苦.