Aller au contenu

vb.net organisé un export vers excel


Nathanalex

Messages recommandés

Membre, Posté(e)
Nathanalex Membre 14 messages
Baby Forumeur‚
Posté(e)

Bonjours,

Afin de realisé un projet, je voudrais pouvoir exporter le contenue d'un datagridview vers excel ( ce qui fonctionne tres bien) mais je voudrais pouvoir organisé mon export.

Je voudrais que au bout des 5 ligne exporter, la 6eme se mettre dans la colonne A dans la ligne en dessous de la ligne ou la premiere info a ete ecrite .

En clairs, que les lignes de mon datagrid soit exporter de la façon suivante :

| Col A | Col B | C | D | E

___________________________________________________

1 | Ligne 1 | Ligne 2 | Ligne 3 | Ligne 4 | Ligne 5

___________________________________________________

2 | Ligne 6 | Ligne 7 | Ligne 8 | Ligne 9 |Ligne 10

___________________________________________________

J'ai chercher sur plusieurs forum français ou anglais mais impossible de trouver comment faire

Merci a tous

Lien à poster
Partager sur d’autres sites

  • 3 mois après...
Annonces
Maintenant
Membre, Posté(e)
Doraken Membre 2 messages
Baby Forumeur‚
Posté(e)

Hello ,

J'ai pas la solution clé en main pour ton problème mais j'ai un module qui me sert à manipuler les interactions entre le Datagridwiew et excel .

Regarde :

Module Datagrid_manager_module

Function Clear_datagride(ByVal _used_datagrid As Object) As Boolean
' Efface le composant datagrid '
   	Dim _max_row As Integer
   	_max_row = _used_datagrid.Rows.GetRowCount(System.Windows.Forms.DataGridViewElementStates.Visible)
   	Try
       	If _max_row > 0 Then
           	Do Until _max_row = 1
               	_used_datagrid.Rows.RemoveAt(_used_datagrid.Rows.GetFirstRow(System.Windows.Forms.DataGridViewElementStates.Visible))
               	_max_row = _max_row - 1
           	Loop
       	End If
   	Catch err As Exception
       	MsgBox(err.ToString)
       	Return False
   	End Try
   	Return True
End Function 

Function Write_NewLine_datagrid(ByVal _used_datagrid As System.Windows.Forms.DataGridView, ByVal _New_Line As Boolean, ByVal _Max_col As Integer, ByVal _col_used_Id As Integer, ByVal _Used_Value As String) As Boolean
' Ecriture d'une ligne dans le datagrid wiev'
   	Dim _current_row_used As Integer
   	If _Max_col < _col_used_Id Then
       	'Throw MsgBox("Error on column used")
       	Return False
   	Else
       	If _New_Line = True Then
           	_used_datagrid.Rows.Add(1)
       	End If
       	_current_row_used = _used_datagrid.Rows.GetLastRow(System.Windows.Forms.DataGridViewElementStates.Visible) - 1
       	_used_datagrid.Item(_col_used_Id, _current_row_used).Value = _Used_Value
   	End If
End Function

Function PushToExcelFromDT(ByVal ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal Datagrid As System.Windows.Forms.DataGridView, Optional ByVal progressbar As System.Windows.Forms.ProgressBar = Nothing) As Boolean
'Export brutal des données du datagridwiev'
   	Dim __result As Boolean = False
   	Try
       	If Not progressbar Is Nothing Then
           	progressbar.Maximum = Datagrid.Rows.Count - 1
       	End If
       	Dim MAX8ROW As Integer = Datagrid.Rows.Count - 1
       	Dim MAXCOL As Integer = Datagrid.ColumnCount - 1

       	Dim intRow As Integer = 1
       	For __rows As Integer = 0 To MAX8ROW Step 1

           	Dim IntCol As Integer = 1
           	For __col As Integer = 0 To MAXCOL Step 1


               	If Datagrid.Rows(__rows).Cells(__col).Value Is Nothing Then

               	Else
                   	ExcelSheet.Cells(intRow, IntCol).value = Datagrid.Rows(__rows).Cells(__col).Value.ToString

               	End If
               	IntCol = IntCol + 1

           	Next
           	intRow = intRow + 1
           	If Not progressbar Is Nothing Then
               	progressbar.Increment(1)
           	End If
       	Next
       	__result = True
   	Catch ex As Exception
       	__result = False
   	End Try
   	Return __result
End Function


Function PushTitleToExcel(ByVal ExcelSheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal Datagrid As System.Windows.Forms.DataGridView) As Boolean
  	'Permet de generer une ligne de titre à partir des titre du datagridwiew '
   	Dim __result As Boolean = False
   	Dim cols As System.Windows.Forms.DataGridViewColumnCollection
   	cols = Datagrid.Columns
   	ExcelSheet.Rows("1:1").Insert()

   	Dim ColCount As Integer = 1
   	Try

       	For Each _col As System.Windows.Forms.DataGridViewColumn In cols
           	If _col.HeaderText = Nothing Then
           	Else

               	ExcelSheet.Cells(1, ColCount).Value = _col.HeaderText.ToString
               	ColCount = ColCount + 1
           	End If

       	Next
       	ExcelSheet.Columns.AutoFit()
       	ExcelSheet.Rows.AutoFit()

       	__result = True
   	Catch ex As Exception
       	__result = False
   	End Try
   	Return __result
End Function

End Module 

Le code n'est pas parfait mais en jouant sur la fonction push to excel tu devrais pas avoir trop de soucie pour expédier tes donné dans le bon ordre .

Doraken

Lien à poster
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×