Jared
Jared

Reputation: 2954

CSV export class for datagrids in silverlight

I have been trying to export some datagrids in Silverlight to excel, but have been unsuccesful so far. So now I am just trying to get them to export as a csv file that can then be opened in excel. I have a class created to export the datagrids and it does export and save a csv file. However, the csv file contains no data.

Imports System.IO

Public Class clsExportData
   'Public Shared Sub Export(dg As DataGrid)
   '    SaveExportedGrid(ExportDataGrid(True, dg))
   'End Sub


Public Shared Sub Export(dg As DataGrid, withHeaders As Boolean)
    SaveExportedGrid(ExportDataGrid(withHeaders, dg))
End Sub

Private Shared Sub SaveExportedGrid(data As String)
    Dim sfd As New SaveFileDialog()
    sfd.DefaultExt = "csv"
    sfd.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*"
    sfd.FilterIndex = 1

    If If(sfd.ShowDialog(), False) Then
        Using sr As New StreamWriter(sfd.OpenFile())
            sr.Write(data)
        End Using
    End If
End Sub

Private Shared Function ExportDataGrid(withHeaders As Boolean, grid As DataGrid) As String
    Dim colPath As String
    Dim propInfo As System.Reflection.PropertyInfo
    Dim binding As System.Windows.Data.Binding
    Dim strBuilder As New System.Text.StringBuilder()
    Dim source As System.Collections.IList = TryCast(grid.ItemsSource, System.Collections.IList)
    If source Is Nothing Then
        Return ""
    End If

    Dim headers As New List(Of String)()
    grid.Columns.ToList().ForEach(Function(col)
                                      If TypeOf col Is DataGridBoundColumn Then
                                          headers.Add(FormatCSVField(col.Header.ToString()))
                                      End If

                                  End Function)
    strBuilder.Append([String].Join(",", headers.ToArray())).Append(vbCr & vbLf)

    For Each data As [Object] In source
        Dim csvRow As New List(Of String)()
        For Each col As DataGridColumn In grid.Columns
            If TypeOf col Is DataGridBoundColumn Then
                binding = TryCast(col, DataGridBoundColumn).Binding
                colPath = binding.Path.Path
                propInfo = data.[GetType]().GetProperty(colPath)
                If propInfo IsNot Nothing Then
                    csvRow.Add(FormatCSVField(propInfo.GetValue(data, Nothing).ToString()))
                End If
            End If
        Next
        strBuilder.Append([String].Join(",", csvRow.ToArray())).Append(vbCr & vbLf)
    Next

    Return strBuilder.ToString()
End Function

Private Shared Function FormatCSVField(data As String) As String
    Return [String].Format("""{0}""", data.Replace("""", """""""").Replace(vbLf, "").Replace(vbCr, ""))
End Function

End Class

Upvotes: 0

Views: 455

Answers (1)

Chui Tey
Chui Tey

Reputation: 5564

Try the following instead

Dim source As System.Collections.IEnumerable = TryCast(grid.ItemsSource, System.Collections.IEnumerable)
If source Is Nothing Then
    Return ""
End If

Also, make sure you step through your code with a debugger, and see where it failed.

Upvotes: 1

Related Questions