Excel Process not closing in VB.net

后端 未结 8 1108
星月不相逢
星月不相逢 2020-12-03 09:00

I am creating an excel file using interop.excel and the process is not closing. This is the code i am trying to use.

 Private Sub converToExcel(fileLoc As S         


        
8条回答
  •  无人及你
    2020-12-03 09:22

    'Get the PID from the wHnd and kill the process.
    ' open the spreadsheet
    ImportFileName = OpenFileDialog1.FileName
    excel = New Microsoft.Office.Interop.Excel.ApplicationClass
    wBook = excel.Workbooks.Open(ImportFileName)
    hWnd = excel.Hwnd
    Dim id As Integer = GetWindowThreadProcessId(hWnd, ExcelPID)
    

    Sub CloseExcelFile()
            Try
                ' first try this
                wBook.Saved = True
                wBook.Close()
                excel.Quit()
    
                ' then this.
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
                excel = Nothing
    
                ' This appears to be the only way to close excel!
                Dim oProcess As Process
                oProcess = Process.GetProcessById(ExcelPID)
                If oProcess IsNot Nothing Then
                    oProcess.Kill()
                End If
    
            Catch ex As Exception
                excel = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
    

提交回复
热议问题