关闭

VB.NET中CSV导入DATAGRID

标签: datagridvb.netcsvintegerstringc
2639人阅读 评论(0) 收藏 举报

'=======IO.StreamReader直接读取CSV
        Dim intColCount As Integer = 0
        Dim blnFlag As Boolean = True
        Dim mydt As New DataTable

        Dim mydc As DataColumn
        Dim mydr As DataRow

        Dim strpath As String = "C:/ExportData.csv"
        Dim strline As String
        Dim aryline() As String

        Dim i As Integer
        Dim rowNum As Integer

        Dim mysr As System.IO.StreamReader = New System.IO.StreamReader(strpath, System.Text.Encoding.GetEncoding("GB2312"))
        strline = mysr.ReadLine()
        rowNum = 1
        While (strline <> Nothing)
            aryline = Split(strline, ",")
            If rowNum = 1 Then
                If (blnFlag = True) Then
                    blnFlag = False
                    intColCount = aryline.Length
                    For i = 0 To aryline.Length - 1
                        mydc = New DataColumn(aryline(i))
                        mydt.Columns.Add(mydc)
                    Next
                End If
            Else
                mydr = mydt.NewRow()
                For i = 0 To intColCount - 1
                    mydr(i) = aryline(i)
                Next
                mydt.Rows.Add(mydr)
            End If
            strline = mysr.ReadLine()
            rowNum = rowNum + 1
        End While
        ShowDemo.DataSource = mydt
        ShowDemo.DataBind()

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:240369次
    • 积分:3609
    • 等级:
    • 排名:第9041名
    • 原创:123篇
    • 转载:46篇
    • 译文:0篇
    • 评论:23条
    最新评论