我正在尝试将一些数据从我的windowsform写入Excel
文件,这样可行。
' Excel load data
Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
oWorkSheet = oWorkBook.Worksheets(1)
oWorkSheet.Range("A1").Value = "Test"
oWorkBook.Save()
oWorkBook.Close()
问题是:当我完成时Excel
仍然在我的任务管理器中运行。当我按下按钮10次时,我的任务管理器中有10个Excel
引用。
问题:如何在将值写入Excel
之后完全卸载Excel
?
答案 0 :(得分:2)
您需要Quit
Microsoft Excel,然后释放对象。
此回答引用的代码:The proper way to dispose Excel com object using VB.NET?和Excel application not quitting after calling quit
'Excel load data
Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
oWorkSheet = oWorkBook.Worksheets(1)
oWorkSheet.Range("A1").Value = "Test"
oWorkBook.Save()
oWorkBook.Close()
oExcelApp.Quit()
'Release object references.
releaseObject(oWorkSheet)
releaseObject(oWorkBook)
releaseObject(oExcelApp)
----------------------------------------------------------------------------
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
答案 1 :(得分:0)