使用.net 调用Excel 生成Excel 报表 的时候 会出现 Excel.exe的进程不能关闭. 反复打开几次就会多几次Excel.exe的问题.
... ....
GC.Collect();
... ....
可是这样的方法有时不起作用.
直被一个问题困扰就是导出excel时如何关闭excel进程,我使用过oExcelApp.Quit(); 也用过GC回收,结果都不理想,后来发现可以kill进程,但是问题是kill进程 时不好解决多人并发的使用,比如一个人在导表然后kill所以的excel但是如果同时又有人在导表那么这就把另外一个excel结束了,现在我们要办的 是如何kill当前这个进程,这里我们先看一下代码:
Public Function CloseExcel(ByRef _xl As Excel.Application) As Boolean If _xlApp Is Nothing Then Return True End If If _xlSheet Is Nothing Then Return True End If Try Dim PreocessExcelId As Integer = 0 BringWindowToTop(_xlApp.Hwnd) If _xlBook IsNot Nothing Then Try _xlBook.Close() Catch ex As Exception If Not (TypeOf (ex) Is COMException) Then Throw ex End If End Try End If _xlApp.Quit() GetWindowThreadProcessId(_xlApp.Hwnd,PreocessExcelId) If PreocessExcelId > 0 Then Dim pExcel = Process.GetProcessById(PreocessExcelId) If pExcel IsNot Nothing Then pExcel.Kill() Return True End If End If Catch ex As Exception '_xlBook.Close() '_xlApp.Quit() Finally _xlSheet = Nothing _xlBook = Nothing _xlApp = Nothing End Try End Function <DllImport("User32.dll")> _ Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr,ByRef OutPresId As Integer) As Integer End Function 'BringWindowToTop <DllImport("User32.dll")> _ Private Shared Function BringWindowToTop(ByVal hWnd As IntPtr) As Boolean End Function原文链接:https://www.f2er.com/vb/262071.html