Monday, August 1, 2011

Export Listview to CSV

If you want to export the Listview Data to CSV in Visual Basic 2010 use the function bellow:


Function ExportListview2Excel(ByVal lstview As ListView) As Boolean

    Dim csvFileContents As New System.Text.StringBuilder
    Dim CurrLine As String = String.Empty


    For columnIndex As Int32 = 0 To lstview.Columns.Count - 1

    CurrLine &= (String.Format("{0};", lstview.Columns(columnIndex).Text))

    Next


    csvFileContents.AppendLine(CurrLine.Substring(0, CurrLine.Length - 1))

    CurrLine = String.Empty


    For Each item As ListViewItem In lstview.Items


    For Each subItem As ListViewItem.ListViewSubItem In item.SubItems


    CurrLine &= (String.Format("{0};", subItem.Text))


    Next

    csvFileContents.AppendLine(CurrLine.Substring(0, CurrLine.Length - 1))


    CurrLine = String.Empty


    Next


    Dim Sys As New System.IO.StreamWriter("C:\Test.csv")


    Sys.WriteLine(csvFileContents.ToString)


    Sys.Flush()


    Sys.Dispose()


End Function

 

No comments:

Post a Comment

There was an error in this gadget