24 | 05 | 2017

Export Listview to CSV file .NET

Function below Exports a Listview to a Comma Separated Value file.

Function ExportListview2Excel(ByVal lstview As ListView) As Boolean

Dim csvFileContents As New System.Text.StringBuilder

Dim CurrLine As String = String.Empty

'Write out the column names as headers for the csv file.

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

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

Next

'Remove trailing comma

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

CurrLine = String.Empty

'Write out the data.

For Each item As ListViewItem In lstview.Items

For Each subItem As ListViewItem.ListViewSubItem In item.SubItems

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

Next

'Remove trailing comma

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

CurrLine = String.Empty

Next

'Create the file.

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

Sys.WriteLine(csvFileContents.ToString)

Sys.Flush()

Sys.Dispose()

End Function
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2258909
Latest Articles