這個是我目前試過
真的有馬上回收的方式
之前想只要能直接讀到正在執行的PID
然後Kill它就可以...
結果試了下面的方式....
只是這個讀到的不是目前被呼叫的Excel....
所以還是無效
也不確定有沒有方式可以讀的到就是了....
'要讀取的 Dim PName As String = "Excel".ToUpper() Dim processList() As Process = Process.GetProcesses Dim intPID As Integer For i As Integer = 0 To processList.Length - 1 If processList(i).ProcessName = PName Then intPID = processList(i).Id Exit For End If Next Dim proc As System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(intPID) proc.Kill()
後來有看到
http://www.dotblogs.com.tw/yc421206/archive/2009/07/16/9553.aspx
用GC.Collect()強制回收
只是不懂的話GC最好不能亂用....
我也不是很懂啦....有看過書上寫的很詳細只是沒多久就忘了
不過這個真的直接就強制回收了
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '強制回收被開啟的Excel(注意GC.Collect()不可放在同一個Method裡面) Export() GC.Collect() End Sub Private Sub Export() Dim ExcelAP As New Excel.Application Dim ExcelWB As Excel.Workbook Dim ExcelWS As Excel.Worksheet ExcelAP.AskToUpdateLinks = False ExcelAP.DisplayAlerts = False ExcelAP.Visible = True ExcelWB = ExcelAP.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet) ExcelWB.Activate() ExcelWS = ExcelWB.Worksheets(1) ExcelWS.Name = "Test_Sheet" ExcelWS.Activate() ExcelWS.Cells(1, 1) = "aaa" ExcelWB.SaveAs("D:\test.xls") '下面這些一定要.... ExcelWB.Close() ExcelAP.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelAP) ExcelWS = Nothing ExcelWB = Nothing ExcelAP = Nothing End Sub
全站熱搜
留言列表