By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Process EXCEL.exe never close ! Help please

P: 1
Hello,

I've got a problem on my last code made for take data on SQL and put it on a specific worksheet in Excel.

The process Excel.exe is never close !

Any idea please ?

Expand|Select|Wrap|Line Numbers
  1. Module Module1
  2.  
  3.     Sub Main()
  4.  
  5.         Dim xlApp As New xls.Application
  6.         Dim xlBook As xls.Workbook
  7.         Dim xlSheet As xls.Worksheet
  8.  
  9.         Dim Connexion As New SqlConnection("Data Source=localhost;Initial Catalog=bdd_name;User Id=sa;Password=159753;")
  10.         Dim Requete As String = "SELECT * FROM Table_test"
  11.  
  12.         xlBook = xlApp.Workbooks.Open("C:\Users\toto\Desktop\Classeur1.xlsx")
  13.         xlSheet = xlBook.Worksheets("test")
  14.  
  15.         'Connexion  la BDD
  16.         Connexion.Open()
  17.  
  18.         Dim Commande As New SqlCommand(Requete, Connexion)
  19.         Dim Adaptateur As New SqlDataAdapter(Commande)
  20.         Dim MonDataSet As New DataSet
  21.  
  22.         Try
  23.             Adaptateur.Fill(MonDataSet, "test")
  24.         Catch ex As Exception
  25.  
  26.             Console.WriteLine(ex.Message)
  27.             Commande.Dispose()
  28.             Connexion.Close()
  29.         End Try
  30.  
  31.         Commande.Dispose()
  32.         Connexion.Close()
  33.  
  34.         Dim RowInd As Int16 = 1
  35.         Dim ColInd As Int16 = 1
  36.  
  37.         xlSheet.Range("A1", "Z5000").Value = ""
  38.  
  39.         For ColInd = 0 To 4
  40.             For Each Ligne As DataRow In MonDataSet.Tables("Alarms").Rows()
  41.                 xlSheet.Cells(RowInd, ColInd + 1).Value = Ligne(ColInd).ToString
  42.                 RowInd += 1
  43.             Next
  44.             RowInd = 1
  45.         Next
  46.  
  47.         xlApp.DisplayAlerts = False
  48.         xlSheet.SaveAs("C:\Users\toto\Desktop\Classeur1.xlsx")
  49.         xlBook.Close(False)
  50.         xlApp.Quit()
  51.         xlApp.DisplayAlerts = True
  52.  
  53.         ReleaseObject(xlApp)
  54.         ReleaseObject(xlBook)
  55.         ReleaseObject(xlSheet)
  56.  
  57.         xlApp = Nothing
  58.         xlBook = Nothing
  59.         xlSheet = Nothing
  60.     End Sub
  61.  
  62.  
  63.     Private Sub ReleaseObject(ByVal obj As Object)
  64.         Try
  65.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  66.             obj = Nothing
  67.         Catch ex As Exception
  68.             obj = Nothing
  69.         Finally
  70.             GC.Collect()
  71.         End Try
  72.     End Sub
  73. End Module
Feb 5 '16 #1
Share this Question
Share on Google+
1 Reply


100+
P: 293
You hav to release each RCW object you hav created using marshal.releasecomobject.

Hope this helps.
Feb 9 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.