bdl20042000
Messages postés63Date d'inscriptionmercredi 18 août 2004StatutMembreDernière intervention19 mai 2009
-
18 mai 2009 à 09:24
bdl20042000
Messages postés63Date d'inscriptionmercredi 18 août 2004StatutMembreDernière intervention19 mai 2009
-
19 mai 2009 à 13:38
Bonjour,
je suis sous VS 2008 SP1 et Windows XP SP2.
J'ai apporté une mise en forme sur des cellules de la datagridview - jusqu'à là tout va bien.
C'est une gestion de planning.
Si une personne est en congé ce jour là, je mets la couleur Rouge dans cette cellule.
Je veux ensuite exporter rapidement le dgv vers Excel en gardant le mise en forme.
Cependant :
For Each cell As DataGridViewCell In DataGridView1.Rows(i).Cell
cell.Style.BackColor.ToString ' -> m'affiche toujours "Color [Empty]"
Next
Dans le hearder, c'est DataGridView1.Columns(0).defaultcellstyle.backcolor et j'ai la couleur.
Mais peut être que c'est autre chose pour Cell ?
merci pour tout aide que quelqu'un pourrait m'apporter.
bdl20042000
Messages postés63Date d'inscriptionmercredi 18 août 2004StatutMembreDernière intervention19 mai 2009 19 mai 2009 à 13:38
bonjour,
désolé, mais à priori cela fonctionne maintenant chez moi.
Voici le code qui permet d'exporter un datagridview vers excel en conservant les valeurs et la couleur de la cellule :
Imports Microsoft.Office.Interop
Imports System.Text
' Nom des champs
For i As Int32 = 0 To nbrColonne
j = InStr(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString, \"[\")
couleur = Mid(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString, j + 1, Len(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString) - j - 1)
html.AppendLine(\"" _
+ DataGridView1.Columns(i).HeaderText + ", \")
Next
If nbrLigne > -1 Then
For i As Int32 = 0 To nbrLigne
' Remplissage des lignes
html.AppendLine(\"----
\")
For Each cell As DataGridViewCell In DataGridView1.Rows(i).Cells
j = InStr(cell.Style.BackColor.ToString, \"[\")
couleur = Mid(cell.Style.BackColor.ToString, j + 1, Len(cell.Style.BackColor.ToString) - j - 1)
If couleur <> \"Empty\" Then
html.AppendLine(\"" + ", \")
Else
html.AppendLine(\", \")
End If
Next
Next
End If
html.AppendLine("
</center></html>")
Clipboard.SetText(html.ToString)
'Variables for Excel.
Dim xlApp As New Excel.Application
Dim xlWBook As Excel.Workbook = xlApp.Workbooks.Add( _
Excel.XlWBATemplate.xlWBATWorksheet)
Dim xlWSheet As Excel.Worksheet = CType(xlWBook.Worksheets(1), Excel.Worksheet)
Dim xlCalc As Excel.XlCalculation
'Save the present setting for Excel's calculation mode and turn it off.
With xlApp
xlCalc = .Calculation
.Calculation = Excel.XlCalculation.xlCalculationManual
End With
With xlWSheet
.Paste()
.UsedRange.Columns.AutoFit()
End With
With xlApp
.Visible = True
.UserControl = True
'Restore the calculation mode.
.Calculation = xlCalc
End With
'Relase objects from memory.
xlWSheet = Nothing
xlWBook = Nothing
xlApp = Nothing
GC.Collect()
End Sub