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
|
Public Sub Export_Excel(My_Listview As ListView, Nbr_Lignes As Integer, Nbr_Colonnes As Integer)
Dim Appli As New Excel.Application
Dim Ligne As String
Dim LigneExcel As Integer
Dim ColExcel As Integer
Dim compt As Integer
Dim comptcol As Integer
Dim fso As New FileSystemObject
'Rendre visible EXCEL
Appli.Visible = True
'Créer un nouveau classeur EXCEL initialisé à la ligne 1
Appli.Workbooks.Add.Activate
DoEvents
LigneExcel = 1
ColExcel = 1
'MsgBox Appli.Workbooks.Count
' Affecter les données de la listbox dans les cellules de la feuille
With ActiveWorkbook.Worksheets("Feuil1")
'Insere le nom des entetes de colonnes
For comptcol = 0 To Nbr_Colonnes - 1
.Cells(LigneExcel, ColExcel) = My_Listview.ColumnHeaders(comptcol + 1)
ColExcel = ColExcel + 1
Next comptcol
ColExcel = 1
LigneExcel = LigneExcel + 1
'Inscrire le contenu d'une listview dans la feuille 1 d'un classeur EXCEL
For compt = 0 To My_Listview.ListItems.Count - 1
'On boucle sur les colonnes
For comptcol = 0 To Nbr_Colonnes - 1
If comptcol = 0 Then
.Cells(LigneExcel, ColExcel) = My_Listview.ListItems.Item(LigneExcel - 1)
Else
.Cells(LigneExcel, ColExcel) = My_Listview.ListItems.Item(LigneExcel - 1).ListSubItems(comptcol)
End If
ColExcel = ColExcel + 1
Next comptcol
ColExcel = 1
LigneExcel = LigneExcel + 1
Next compt
End With
'Pour mettre l'entête des colonnes en gras
ActiveWorkbook.Worksheets("Feuil1").Range("A1:" & Chr(65 + Nbr_Colonnes - 1) & "1").Font.Bold = True
'Pour ajuster les colonnes
ActiveWorkbook.Worksheets("Feuil1").Range("A:" & Chr(65 + Nbr_Colonnes - 1)).Columns.AutoFit
End Sub |
Partager