Bonjour

J'aimerai pouvoir exporter une datagridview vers Excel; ce que j'arrive avec le code proposé plus bas; mais aussi exporter les couleurs des cellules de Datagrid que je retrouverai dans Excel !

Si c'est possible, merci de votre aide...

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
 
Dim ExcelApp As Object, ExcelBook As Object
        Dim ExcelSheet As Object
        Dim i As Integer
        Dim j As Integer
 
        ' Minimum à 1 
        ProgressBar1.Minimum = 0
        ' Maximum= nombre total de fichier à copier.
        'ProgressBar1 = filenames.Length
        ' On initialise la ProgressBar.
        ProgressBar1.Value = 1
        ' On indique le pas.
        ProgressBar1.Step = 6
        ProgressBar1.Style = ProgressBarStyle.Blocks
        ProgressBar1.Visible = True
 
        'create object of excel
        ExcelApp = CreateObject("Excel.Application")
        ExcelBook = ExcelApp.WorkBooks.Add
        ExcelSheet = ExcelBook.WorkSheets(1)
 
        With ExcelSheet
            For Each col As DataGridViewColumn In Me.DataGridView1.Columns
                ExcelSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
                For i = 1 To Me.DataGridView1.RowCount - 1
                    ExcelSheet.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("numero_offre").Value
                    For j = 1 To DataGridView1.Columns.Count - 1
                        'If DataGridView1.Rows(i - 1).Cells(j).Value = "" Then DataGridView1.Rows(i - 1).Cells(j).Value = "/"
                        ExcelSheet.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
                    Next
                Next
                ProgressBar1.PerformStep()
            Next
        End With
        ExcelApp.Visible = True
        ExcelSheet = Nothing
        ExcelBook = Nothing
        ExcelApp = Nothing
 
        ' Save Excel (XLSX) to a file.
        'ExcelBook.Saveas("D:\Mes Documents\Visual Studio 2015\Projects\EMPLOI_Suivi\WindowsApplication1\bin\Debug\export\Export_Excel.xlsx")
 
        System.Threading.Thread.Sleep(3000)
        ProgressBar1.Visible = False
        MessageBox.Show("Excel créé:" & Chr(10) &
                        "- Sauvegardez le dans le Dossier de votre choix !")
        If System.IO.File.Exists(My.Application.Info.DirectoryPath + "\export\Export_Excel.xlsx") Then
            IO.File.Delete(My.Application.Info.DirectoryPath + " \export\Export_Excel.xlsx")
        End If
        ' Minimum à 1 
        ProgressBar1.Minimum = 0
        ' Maximum= nombre total de fichier à copier.
        'ProgressBar1 = filenames.Length
        ' On initialise la ProgressBar.
        ProgressBar1.Value = 1
        ' On indique le pas.
        ProgressBar1.Step = 6
        ProgressBar1.Style = ProgressBarStyle.Blocks
        ProgressBar1.Visible = False