Free Visual Basic 2010 Source Code VB2010
How to Write a ListView to a CSV File








HOW TO WRITE A LISTVIEW TO A CSV FILE USING VISUAL BASIC 2010 VB2010


DESCRIPTION

This Visual Basic 2010 VB2010 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 2010.


THE VB2010 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 2010 SOURCE CODE

Copy and Paste this VB2010 code wherever you need it:

'.....Free Source Code from www.freevb2010code.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




Home to freevb2010code.com
VB2010 How to write to a CSV file
VB2010 How to write to a TEXT file
VB2010 How to read from a CSV file
VB2010 How to read from a TEXT file
VB2010 How to find a User Name
VB2010 How to fill a ListView
VB2010 How to write a ListView data table to a CSV file
VB2010 How to call another program
VB2010 How to copy and paste to the Windows Clipboard
VB2010 How to sort an array
VB2010 How to concatenate / join strings / text


Copyright SGDesigns (c)2010

Our aim is to provide simple concise and easy to follow Free Visual Basic 2010 Source Code for the casual programmer in VB2010. We hope you find the Visual Basic 2010 code example uselful. This code shows a simple method on how to write a data table from a ListView to a csv file using Visual Basic 2010.





counter