tags:

views:

240

answers:

2

Dear Friend Below is my export to csv from listview using vb.net

Function ExportListview2CSV(ByVal lstview As ListView) As Boolean Dim saveFileDialog1 As New SaveFileDialog() Dim csvFileContents As New System.Text.StringBuilder Dim CurrLine As String = String.Empty saveFileDialog1.Filter = "CSV|*.csv" saveFileDialog1.Title = "Save an CSV File"

    csvFileContents.AppendLine("Service Provider Name: |" & cbodestproname.Text)
    'csvFileContents.AppendLine(cbodestproname.Text)

    csvFileContents.AppendLine("Circel Name: |" & cbodestcirclename.Text)
    'csvFileContents.AppendLine(cbodestcirclename.Text)

    csvFileContents.AppendLine("Month: |" & dtpDate.Text)
    '  csvFileContents.AppendLine(dtpDate.Text)

    csvFileContents.AppendLine("Type of File: |" & cbotypeoffile.Text)
    '  csvFileContents.AppendLine(cbotypeoffile.Text)


    csvFileContents.AppendLine("")
    'Write out the column names as headers for the csv file.
    For columnIndex As Int32 = 1 To lstview.Columns.Count - 2
        CurrLine &= (String.Format("{0}|", lstview.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 lstview.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.
    If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        If saveFileDialog1.FileName <> "" Then
            Dim Sys As New System.IO.StreamWriter(saveFileDialog1.FileName)
            Sys.WriteLine(csvFileContents.ToString)
            Sys.Flush()
            Sys.Dispose()
            MsgBox("Data's are Saved Succesfully to " & saveFileDialog1.FileName, MsgBoxStyle.Information)
        End If
    End If


End Function

i want to exclude the records from the 1st and last rows where should i make chages in above code

plz help me

thanx in advance.

+1  A: 

You might use

For i as Integer = 1 to lstview.Items.Count - 2

...

Next

that will start from the second item and stop before the last item

michele
A: 

yeah, you should use an index other than for each to iterate the items in listview

Benny