9/01/2011

VB.Net : ส่งค่า ListView ออกไปไฟล์ CSV

use code in button and rename ListView :

       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 ListView_Sales.Columns.Count - 1
            CurrLine &= (String.Format("{0};", ListView_Sales.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 ListView_Sales.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()

No comments:

Post a Comment