VB. NET cannot close the excel.exe process by calling EXCEL.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.