用户打开Excel文件进行数据输入后关闭Windows中的Excel进程
程序打开Excels,读取工作表并填充每个工作表中的数据数组。对于每个打开以获取输入数据的Excel,Windows进程列表中将有一个Excel进程。如果用户在运行时一段时间内打开了许多Excel,那么将有许多Excel进程在Windows中运行 问题是,如果您转到一个文件夹并尝试打开一个以编程方式打开的Excel,它将是只读的,因为在Windows中运行的Excel进程将保留它。需要终止Windows中的每个Excel进程,才能释放通过编程打开的每个Excal用户打开Excel文件进行数据输入后关闭Windows中的Excel进程,excel,marshalling,vb.net-2010,Excel,Marshalling,Vb.net 2010,程序打开Excels,读取工作表并填充每个工作表中的数据数组。对于每个打开以获取输入数据的Excel,Windows进程列表中将有一个Excel进程。如果用户在运行时一段时间内打开了许多Excel,那么将有许多Excel进程在Windows中运行 问题是,如果您转到一个文件夹并尝试打开一个以编程方式打开的Excel,它将是只读的,因为在Windows中运行的Excel进程将保留它。需要终止Windows中的每个Excel进程,才能释放通过编程打开的每个Excal Imports Excel =
Imports Excel = Microsoft.Office.Interop.Excel
' Create new Application.
Dim rXL As New Excel.Application
Dim rWB As Excel.Workbook
' Dim rSheet As Excel.Worksheet
Dim rRng As Excel.Range
Dim buffmatrix(,) As Object
rWB = rXL.Workbooks.Open(filename)
Dim sheet As Excel.Worksheet
For i = 1 To rWB.Sheets.Count To 1 Step -1
' Get sheet.
sheet = rWB.Sheets(i)
' Get range.
Dim r As Excel.Range = sheet.UsedRange
If r.Rows.Count = 0 Then Exit For
' Load all cells into 2d array.
Dim eCellArray As System.Array = r.Value
' eCellArray is used for processing here
Marshal.ReleaseComObject(Sheet)
rRng = Nothing
Next
rWB = Nothing
rXL = Nothing
上面循环中的Marshal.ReleaseComObjectSheet命令不起作用,也就是说,它没有在Windows中终止Excel进程。我使用此代码解决了此问题
WorkBook As Microsoft.Office.Interop.Excel.Workbook WorkBook
ExcelInstance As Microsoft.Office.Interop.Excel.ApplicationClass
WorkBook.Close(_missing, _missing, _missing)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WorkBook)
ExcelInstance.Quit()
ExcelInstance.Application.Quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ExcelInstance)
GC.Collect()
GC.WaitForPendingFinalizers()
工作很有魅力-谢谢。打开这么多Excel进程是非常烦人的。