Bonjour, je reviens vers vous car j'ai le problème suivant avec mon code :

Le processus EXCEL.EXE reste ouvert après l’exécution du code, des idées ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Imports System.Data.SqlClient
Imports xls = Microsoft.Office.Interop.Excel
 
Module Module1
 
    Sub Main()
 
        Dim xlApp As New xls.Application
        Dim xlBook As xls.Workbook
        Dim xlSheet As xls.Worksheet
 
        'Configuration de la connection SQL + Requete   
        Dim Connexion As New SqlConnection("Data Source=localhost;Initial Catalog=bdd_name;User Id=sa;Password=159753;")
        Dim Requete As String = "SELECT * FROM Table_test"
 
        'Configuration du fichier Excel ainsi que de la Worksheet à modifier
        xlBook = xlApp.Workbooks.Open("C:\Users\toto\Desktop\Classeur1.xlsx")
        xlSheet = xlBook.Worksheets("test")
 
        'Connexion à la BDD
        Connexion.Open()
 
        Dim Commande As New SqlCommand(Requete, Connexion)
        Dim Adaptateur As New SqlDataAdapter(Commande)
        Dim MonDataSet As New DataSet
 
        Try
            'Récupération des données dans un DataSet
            Adaptateur.Fill(MonDataSet, "test")
        Catch ex As Exception
            'Fermeture de la connection en cas d'erreur           
            Console.WriteLine(ex.Message)
            Commande.Dispose()
            Connexion.Close()
        End Try
 
        'Fermeture de la connection
        Commande.Dispose()
        Connexion.Close()
 
        'Ajout du dataset dans le fichier Excel
        Dim RowInd As Int16 = 1
        Dim ColInd As Int16 = 1
 
        xlSheet.Range("A1", "Z5000").Value = ""
 
        For ColInd = 0 To 4
            For Each Ligne As DataRow In MonDataSet.Tables("Alarms").Rows()
                xlSheet.Cells(RowInd, ColInd + 1).Value = Ligne(ColInd).ToString
                RowInd += 1
            Next
            RowInd = 1
        Next
        'Sauvegarde du fichier Excel et fermeture de la connection
        xlApp.DisplayAlerts = False
        xlSheet.SaveAs("C:\Users\toto\Desktop\Classeur1.xlsx")
        xlBook.Close(False)
        xlApp.Quit()
        xlApp.DisplayAlerts = True
 
        ReleaseObject(xlApp)
        ReleaseObject(xlBook)
        ReleaseObject(xlSheet)
 
        xlApp = Nothing
        xlBook = Nothing
        xlSheet = Nothing
    End Sub
 
 
    Private Sub ReleaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Module