在用户打开 Excel 个文件进行数据输入后关闭 Windows 中的 Excel 个进程
Closing Excel processes in Windows after user opens Excel files for data input
程序打开 Excel 秒,读取 sheet 秒并为每个 sheet 中的数据填充数组。每打开一个Excel获取输入数据,Windows进程列表中就会有一个Excel进程。如果用户在 运行 时间内随着时间的推移打开了许多 Excel,那么在 Windows 中将有许多 Excel 进程 运行ning。
问题是,如果您转到一个文件夹并尝试打开其中一个以编程方式打开的 Excel,由于 Excel 进程 运行ning in Windows 坚持下去。 Windows 中的每个 Excel 进程都需要终止,以便释放以编程方式打开的每个 Excal。
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.ReleaseComObject(Sheet)命令不起作用,也就是说,它没有终止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 秒,读取 sheet 秒并为每个 sheet 中的数据填充数组。每打开一个Excel获取输入数据,Windows进程列表中就会有一个Excel进程。如果用户在 运行 时间内随着时间的推移打开了许多 Excel,那么在 Windows 中将有许多 Excel 进程 运行ning。
问题是,如果您转到一个文件夹并尝试打开其中一个以编程方式打开的 Excel,由于 Excel 进程 运行ning in Windows 坚持下去。 Windows 中的每个 Excel 进程都需要终止,以便释放以编程方式打开的每个 Excal。
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.ReleaseComObject(Sheet)命令不起作用,也就是说,它没有终止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()