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
| Private Sub bt_export_total_antenne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_export_total_antenne.Click
Dim mycommand As New OleDbCommand("SELECT COUNT(*) as NOMBRE, antenne AS ANTENNES FROM T_intervention GROUP BY antenne", ObjetConnection)
Dim MonDataTable As DataTable
Dim MonDataAdapter As OleDbDataAdapter
Dim MonDataSet As New DataSet
MonDataAdapter = New OleDbDataAdapter(mycommand)
MonDataAdapter.Fill(MonDataSet, "t_intervention")
MonDataTable = MonDataSet.Tables("t_intervention")
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
' ici on compte le nombre de lignes et de colonnes du datatable
Dim nbrLigne As Integer = MonDataSet.Tables("t_intervention").Rows.Count - 1
Dim nbrColon As Integer = MonDataSet.Tables("t_intervention").Columns.Count - 1
Dim x, y As Integer
For x = 0 To nbrColon
' ici on prends le titre des colonnes du datatable
xlSheet.Cells(1, x + 1) = MonDataSet.Tables("t_intervention").Columns(x).ColumnName
' on mets la première ligne en gras
xlSheet.Rows(1).Font.Bold = True
' pour chaque colonne et chaque ligne on transfert les données
For y = 0 To nbrLigne
xlSheet.Cells(y + 2, x + 1) = DataGridView1.Item(y, x)
Next
Next
' ici on affiche les résultat dans excel
xlSheet.Application.Visible = True
' on peut sauvegarder notre document sur le disque
xlSheet.SaveAs("C:\nom_document.xls")
' on quitte l'application et on détruit les objets
xlApp.Quit()
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
MonDataAdapter = Nothing
MonDataSet = Nothing
MonDataTable = Nothing
End Sub |