HOW TO WRITE A LISTVIEW TO A CSV FILE USING VISUAL BASIC 2008 VB2008
DESCRIPTION
This Visual Basic 2008 VB2008 code is a simple source code example of writing a
complete table of
data from a ListView directly to a CSV file using Visual
Basic 2008.
|
THE VB2008 SETUP
Create a ListView and write some data to it (See 'Fill A ListView')
Then create a button which will be used to write the CSV file when desired.
(Any method can be used to call the write routine, this is just an example)
Add the following lines to your 'Module1' code (add the Imports as the first
lines above Module1 - ie at the top!)
Imports System
Imports System.IO
Imports System.Collections
Module1:
|
THE FREE VISUAL BASIC 2008 SOURCE CODE
Copy and Paste this VB2008 code wherever you need it:
'.....Free Source Code from www.freevisualbasic2008sourcecode.com
Write the following Sub Routine into your Module 1 so that it can be called
from anywhere else in your program.
Sub WriteListViewToCSVFile()
Dim CSVWriter As New StreamWriter("C:\Test.csv")
For i As Integer = 0 To Main.ListView1.Items.Count - 1
For j As Integer = 0 To Main.ListView1.Columns.Count - 1
CSVWriter.Write(Main.ListView1.Items(i).SubItems(j).Text)
Next
CSVWriter.WriteLine()
Next
CSVWriter.Close()
End Sub
Main Form Code
'Add a button to your form and call the new subroutine when the button is
pressed.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
WriteListViewToCSVFile()
End Sub
|
|