Bonjour, je développe une application desktop qui a pour but d'exporter mes données visualisées sur la grid view vers un fichier excel , la visualisation marche bien et j'arrive a afficher les données de ma base vers la grid view , mais quand je clique sur le bouton exporter ca s'arrête et ca me génére cette exception "System.Runtime.InteropServices.COMException*: 'Index non valide. (Exception de HRESULT : 0x8002000B (DISP_E_BADINDEX))'"
qui est en relation avec cette ligne du code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
xlWorkSheet = xlWorkBook.Sheets("sheet1")
quelqu'un pourrait me dire quel est le problème s'il vous plaît ?
voici le code en entier :
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
 
Imports MySql.Data.MySqlClient
Imports System.Data.DataTable
Imports Microsoft.Office.Interop
 
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If MsgBox("Voulez vous vraiment quitter l'application ?", vbYesNo, "Quitter") = vbYes Then
            End
        End If
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Exporter.Click
        Dim i, j As Integer
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
 
        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        Dim con As New MySqlConnection("server=localhost; user id= root; password= ; database= user;")
        con.Open()
        Dim com As String
        com = "SELECT * FROM User"
        Dim dscmd As New MySqlDataAdapter(com, con)
        Dim ds As New DataSet
        dscmd.Fill(ds)
 
        For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                xlWorkSheet.Cells(i + 1, j + 1) =
                ds.Tables(0).Rows(i).Item(j)
            Next
        Next
 
        xlWorkSheet.SaveAs("C:\Users\camo\Desktop\vbexcel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()
 
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)
 
        con.Close()
        MsgBox("You can find the file C:\Users\camo\Desktop\vbexcel.xlsx")
    End Sub
 
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Visualiser.Click
        Dim con As New MySqlConnection("server=localhost; user id= root; password= ; database= user;")
        con.Open()
        Dim com As String
        com = "select * from statistiques "
        Dim adapter As New MySqlDataAdapter(com, con)
        Dim aze As New DataTable()
        adapter.Fill(aze)
        DataGridView1.DataSource = aze
        con.Close()
    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 Class
Merci d'avance