从循环中的第一行和最后一行中排除记录

时间:2009-08-24 08:15:39

标签: vb.net

以下是使用vb.net从listview导出到csv

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

我想从第1行和最后一行中排除记录 我应该在上面的代码中使用哪个字符

请帮帮我

提前完成。

2 个答案:

答案 0 :(得分:1)

您可以使用

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

...

Next

将从第二个项目开始并在最后一个项目之前停止

答案 1 :(得分:0)

是的,您应该使用除每个索引之外的索引来迭代listview中的项目