-
Saving Listview as (.csv) file
Posted on July 16th, 2009 3 commentsIt was easy for me to save a file on the database but saving a file as a (.csv) format is another issue for me before. Until I discover saving as a (.csv) format is just as simple as saving file on a database. All you just need to do on this is that to call the streamwriter to save the data as (.csv) format. I have here a sample codes below where the data the loaded on my listview was saved as (.csv) format. Hope you enjoy.
Private Sub SaveAsFile()
‘They want to do a SaveAs, so find out what they want to name the file
Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()
saveFileDialog.Title = “Save File” p>
saveFileDialog.Filter = “Files (*.csv)|*.csv|All Files (*.*)|*.*”
saveFileDialog.DefaultExt = “csv”
saveFileDialog.AddExtension = True
If saveFileDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
filename = saveFileDialog.FileName
End If
Try
Dim csvFileContents As New System.Text.StringBuilder
Dim currentLine As String = String.Empty‘Write out the column names as headers for the csv file.
For columnIndex As Int32 = 0 To 0
currentLine &= (String.Format(”mycolumn_name”))
Next‘Remove trailing comma
csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length))
currentLine = String.Empty‘Write out the data.
For Each item As ListViewItem In ListView1.ItemsFor Each subItem As ListViewItem.ListViewSubItem In item.SubItems
currentLine &= (String.Format(”"”{0}”",”, subItem.Text))
Next
‘Remove trailing commacsvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))
currentLine = String.EmptyNext
‘Create the file.
Dim sw As New System.IO.StreamWriter(filename)
sw.WriteLine(csvFileContents.ToString)
sw.Flush()
sw.Dispose()
Catch ex As ExceptionEnd Try
End Sub
3 responses to “Saving Listview as (.csv) file”

-
And to open one file to place it in one Viewlist :S .. could you help me , for example the saved file i made with your code .. please help ..
-
hey , works for me the code but now i need to replace the info inside the Listview1 .. please help me how to do it ..
i added your code about how to save all info and works .. but i dont know how to open the data to place it in the listview again .. :S jo jo j o
Leave a reply
-













Hernan July 23rd, 2009 at 21:15