Export datagrid vers Excel et checkbox
Bonjour,
Je peux facilement exporter les données contenu dans mon datagrid via ce code :
Code:
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 76 77 78 79 80 81 82
| 'verfying the datagridview having data or not
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
MessageBox.Show("Aucunes données à exporter !", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'Creating dataset to export
Dim dset As New DataSet
Dim dset2 As New DataSet
'add table to dataset
dset.Tables.Add()
dset2.Tables.Add()
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
dset.Tables(0).Columns.Add()
Next
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
dr1(j) = DataGridView1.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
If colIndex = 1 Then dc.ColumnName = "NOM"
If colIndex = 2 Then dc.ColumnName = "TYPE"
If colIndex = 3 Then dc.ColumnName = "MARQUE"
If colIndex = 4 Then dc.ColumnName = "N°SERIE"
If colIndex = 5 Then dc.ColumnName = "UTILISATEUR"
If colIndex = 6 Then dc.ColumnName = "LIEU"
If colIndex = 7 Then dc.ColumnName = "DATE D'ACHAT"
If colIndex = 8 Then dc.ColumnName = "GARANTIE"
If colIndex = 9 Then dc.ColumnName = "EXPIRATION GARANTIE"
If colIndex = 10 Then dc.ColumnName = "OBSOLETE"
If colIndex = 11 Then dc.ColumnName = "SYSTEME D'EXPLOITATION"
If colIndex = 12 Then dc.ColumnName = "PROCESSEUR"
If colIndex = 13 Then dc.ColumnName = "DISQUE DUR"
If colIndex = 14 Then dc.ColumnName = "MEMOIRE"
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim strFileName As String = "C:\export.xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True |
Cependant, les deux champs contenant des checkbox dans le datagrid, me donnent des true et false dans mon fichier Excel créé.
Pouvez-vous me dire s'il est possible de récupérer les checkbox dans excel et non leur valeur ?
Merci