在vb中编辑CSV

时间:2013-10-08 12:10:55

标签: vb.net csv

我从早期版本的软件导出CSV并希望将其导入到新版本中,但是我只想从CSV中选择几列并在Windows中单击button1时显示它以大写顺序排列。可以做到这一点以及如何完成。

如果不明白请发消息,并会详细介绍。

我到目前为止这只是显示CSV

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dx As Long = 0
    Dim dRow As Long = 0
    Dim dColumn As Long = 0
    Dim dTotalRows As Long = 0
    Dim dTotalColumns As Long = 0
    Dim dFileName As String = ""
    Dim dReadLine As String = ""
    Dim dChar As String = ""
    Dim dArray(1, 1) As String
    Dim dStart As Long = 1
    Dim dEnd As Long = 1
    Dim dLen As Long = 0
    Dim dLineLength As Long = 0
    Dim dQuoteCounter As Long = 0
    Dim dAdd2ItemList As String = ""
    dFileName = "E:\test docs/test.csv"
    FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
    Do While Not EOF(1)
        dReadLine = LineInput(1)
        dRow = dRow + 1
        dTotalColumns = dColumn
        dColumn = 0
        dLineLength = Len(dReadLine)
        For dx = 1 To dLineLength
            dChar = Mid(dReadLine, dx, 1)
            If dChar = Chr(34) Then
                dQuoteCounter = dQuoteCounter + 1
                If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
                    dChar = Mid(dReadLine, dx, 2)
                    If dChar = Chr(34) + "," Then
                        dColumn = dColumn + 1
                        dQuoteCounter = -1
                        If Chr(34) = dChar + "," Then
                            dQuoteCounter = -1
                            dColumn = dColumn + 1
                        End If
                    End If
                End If
            End If
            If dQuoteCounter = 0 And dChar = "," Then
                dColumn = dColumn + 1
            End If
            If dx = dLineLength Then
                dColumn = dColumn + 1
            End If
            If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
        Next (dx)
    Loop
    dTotalRows = dRow
    ReDim dArray(dTotalRows, dTotalColumns)
    FileClose(1)
    dRow = 0
    FileOpen(1, dFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1)
    Do While Not EOF(1)
        dAdd2ItemList = ""
        dRow = dRow + 1
        dStart = 1
        dEnd = 1
        dLen = 0
        dColumn = 0
        dReadLine = LineInput(1)
        dLineLength = Len(dReadLine)
        For dx = 1 To dLineLength
            dChar = Mid(dReadLine, dx, 1)
            If dChar = Chr(34) Then
                dQuoteCounter = dQuoteCounter + 1
                If dQuoteCounter = 1 Then dStart = dx + 1
                If dQuoteCounter = 2 * (Int(dQuoteCounter / 2)) Then
                    dChar = Mid(dReadLine, dx, 2)
                    If dChar = Chr(34) + "," Then
                        dEnd = dx
                        dLen = dEnd - dStart
                        dColumn = dColumn + 1
                        dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                        dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                        dQuoteCounter = -1
                        dStart = dx + 2
                    End If
                End If
            End If
            If dQuoteCounter = 0 And dChar = "," Then
                dEnd = dx
                dLen = (dEnd - dStart)
                dColumn = dColumn + 1
                If dLen < 1 Then
                    dArray(dRow, dColumn) = ""
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                Else
                    dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn) + "|"
                End If
                dStart = dx + 1
            End If
            If dx = dLineLength Then
                dEnd = dx
                dLen = (dEnd - dStart) + 1
                dColumn = dColumn + 1
                If dLen < 1 Then
                    dArray(dRow, dColumn) = ""
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
                Else
                    dArray(dRow, dColumn) = Mid(dReadLine, dStart, dLen)
                    dAdd2ItemList = dAdd2ItemList + dArray(dRow, dColumn)
                End If
                dStart = dx + 1
            End If
            If dQuoteCounter = -1 And dChar = "," Then dQuoteCounter = 0
        Next (dx)
        ListBox1.Items.Add(dAdd2ItemList)
        REM dRow = dRow + 1
    Loop
End Sub

结束班

0 个答案:

没有答案