When you use. Net to call EXCEL to generate an Excel report, The excel.exe process cannot be closed. When the dig command is opened several times, the excel.exe problem may occur several times.
The method used on the Internet to disable this process.
.......
GC. Collect ();
.......
However, this method sometimes does not work.
I was troubled by how to close the Excel process when exporting an Excel file. I used oexcelapp. quit (); also used GC collection, the results are not ideal, and later found that the kill process can be used, but the problem is that it is difficult to solve the problem of concurrent use of multiple people when the kill process, for example, if a person is exporting a table and then kill the Excel file, but another person is exporting the table at the same time, this will end the other Excel file. What we need to do now is how to kill the current process, here let's take a look at the Code:
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