VB.NET中CSV导入DATAGRID

'=======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()

阅读更多
上一篇VB.NET DATAGRID或DATASET 输出CSV文件
下一篇vs.net2003 连接数据库
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭