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
| Private Sub ExportExcel(ByVal dg As DataGrid)
VerifColonnes(dg)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Dim sw As New StringWriter
Dim html As New HtmlTextWriter(sw)
dg.RenderControl(html)
Response.Write(html.ToString())
Response.End()
End Sub
Private Sub VerifColonnes(ByVal dg As Control)
' On récupère le nombre de controles enfants composant le DataGrid
Dim nbControls As Integer = dg.Controls.Count - 1
While nbControls >= 0
VerifColonnes(dg.Controls(nbControls))
nbControls = nbControls - 1
End While
' Si la cellule ne contient pas du texte simple
If Not (TypeOf dg Is TableCell) Then
' Si le controle concerné à une prorpiété "Selected Item" alors...
' Remarque : Seul les controles offrant des choix multiples ont une propriété "SelectedItem"
If Not (dg.GetType().GetProperty("SelectedItem") Is Nothing) Then
Dim controleLitteral1 As LiteralControl = New LiteralControl
dg.Parent.Controls.Add(controleLitteral1)
Try
' La cellule prend alors pour valeur le texte correspondant à la propriété "SelectedItem"
controleLitteral1.Text = CType(dg.GetType().GetProperty("SelectedItem").GetValue(dg, Nothing), String)
Catch ex As Exception
' On récupère l'exception en cas de problème
Response.Write(ex.Message)
End Try
' Le controle concerné est retiré
dg.Parent.Controls.Remove(dg)
Else
' Si le controle concerné n'a pas de propriété "SelectedItem" alors on récupère le
' texte (s'il y en a un) de sa propriété "Text"
If Not (dg.GetType().GetProperty("Text") Is Nothing) Then
Dim controleLitteral2 As LiteralControl = New LiteralControl
dg.Parent.Controls.Add(controleLitteral2)
' On attribue le texte de la propriété "Text" à la cellule concernée
controleLitteral2.Text = CType(dg.GetType().GetProperty("Text").GetValue(dg, Nothing), String)
dg.Parent.Controls.Remove(dg)
End If
End If
End If
End Sub |
Partager