.NET ListView Control Export To CSV File
By Mark Firth
This code sample will demonstrate how to quickly export the contents of a .NET ListView control to a .csv file for use in applications such as Excel.
I love the listview - low resource requirement, reasonably fast and I have built some utilities to ease the workload.
I am often asked by the users for an export of the grid data. This
would normally require that I keep the table used to populate the
listview in memory and then to format the table to reflect the listview
data.
This assumes that you use a generic table with ids and keys and other
extraneous bits and pieces which you then hide from the user by making
the column width = to 0. If you are one of these people who design the
servicing table to fit the exact requirements of the listview then more
joy to you, this will be of limited use.
The other assumption is that you store the ID for the record in the tag property of the listviewitem.
The call from a menu item to export the data
Private Sub ExportGridToolStripMenuItem_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
ExportGridToolStripMenuItem.Click
Dim sMsg As String
sMsg = gUI.ListviewToCSV(lvData)
MessageBox.Show(sMsg)
End Sub
'A couple of overloads to allow for the hidden fields and a file name
Public Overloads Function ListviewToCSV(ByVal oLV As ListView) As String
Dim sFile As String =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) &
"\ListviewData.csv"
Return ListviewToCSV(oLV, sFile)
End Function
Public Overloads Function ListviewToCSV(ByVal oLV As ListView, ByVal sFile As String) As String
Return ListviewToCSV(oLV, sFile, False)
End Function
'The function that writes out the data to a CSV file
Public Overloads Function ListviewToCSV(ByVal oLV As ListView, ByVal sFile As String, ByVal bIncludeHidden As Boolean) As String
Try
Dim i As Integer
Dim oItem As ListViewItem
Dim sData As String = ""
Dim sLine As String
Dim Q As String = Chr(34)
Dim QC As String = Chr(34) + ","
'Create the header using the column headers
sLine = Q + "ID" + QC
For i = 0 To oLV.Columns.Count - 1
'test for the hidden flag
If bIncludeHidden Or oLV.Columns(i).Width > 0 Then
'remove any spaces from the
header data
sLine += Q +
Replace(oLV.Columns(i).Text, " ", "") + QC
End If
Next
'Drop the trailing comma
sData += DropChar(sLine, 1) + vbNewLine
For Each oItem In oLV.Items
sLine = IIf(IsNumeric(oItem.Tag), oItem.Tag + ",", Q
+ oItem.Tag + QC)
For i = 0 To oItem.SubItems.Count - 1
If bIncludeHidden Or
oLV.Columns(i).Width > 0 Then
'wrap the
nonnumeric fields in quotes
sLine +=
IIf(IsNumeric(oItem.SubItems(i).Text), oItem.SubItems(i).Text + ",", Q
+ oItem.SubItems(i).Text + QC)
End If
Next
sData += DropChar(sLine, 1) + vbNewLine
Next
'Delete any existing file of the same name
Dim oFI As New IO.FileInfo(sFile)
If ofi.Exists Then
oFI.Delete()
End If
Dim oFS As New
IO.FileStream(sFile, IO.FileMode.CreateNew, IO.FileAccess.Write)
Dim oSW As New IO.StreamWriter(oFS)
oSW.Write(sData)
oSW.Flush()
oSW.Close()
oFS.Close()
Return "Listview exported to: " + sFile
Catch Exc As Exception
Return Exc.Message
End Try
End Function
'Oh and the DropChar thing
Public Shared Function DropChar(ByVal sText As String, Optional ByVal iCharToDrop As Integer = 1) As String
Try
Dim sTemp As String
sTemp = Trim(LTrim(sText))
sTemp = Str.Left(sTemp, sTemp.Length - iCharToDrop)
Return sTemp
Catch Exc As Exception
Throw Exc
End Try
End Function
I hope this is a useful snippet.
Mark Firth
Popularity (3306 Views)
Article Discussion: Listview to CSV file
Mark Firth posted at Tuesday, December 19, 2006 4:05 AM
.NET ListView Control Export To CSV FileThis code sample will demonstrate how to quickly export the contents of a .NET ListView control to a .csv file for use in applications such as Excel.