Home General VB.NET Export Listview to CSV file .NET
20 | 05 | 2012
This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade

Export Listview to CSV file .NET PDF Print E-mail
User Rating: / 1
PoorBest 
Tutorials

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

Show/Hidden vbnet code

View source
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

 

Comments   

 
0 #3 Guest 2011-10-02 22:58
Hello.
I think it's best to copy the code in the statement that you need and not a function.

'example:
Private Sub Button1_Click () Handles Button.Click

'The code here ...

End Sub
Quote
 
 
0 #2 Guest 2011-09-21 07:17
how to call this function ???
Quote
 
 
0 #1 Guest 2010-04-19 06:45
:lol: :D :roll:
Quote
 

Add comment


Security code
Refresh

This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade