Qi Zai ~~ ~~
只为成功找方法,不为失败找借口!
 新技术的发展有点应接不暇,用户的胃口则被吊得越来越高,软件开发的质量也越来越高,因此对程序员的代码质量也随之变高。回到正题,今天我要说的是有关读取CSV文件到DataGrid表中,因为我在好几个项目中都用到了,页面很简单。



    
 下面是实现代码:
    Private Sub btnFileLoad_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnFileLoad.Click
        
Dim l_objStreamReader As System.IO.StreamReader
        
Dim l_ArrayData() As String
        
Dim l_strFile As String
        
Dim l_strSinnkbn As String
        
Dim l_strHenkouShainNo As String
        
Dim l_strHenkouShainName As String
        
Dim l_strShainNo As String
        
Dim l_strShainMei As String
        
Dim l_strBmncode As String
        
Dim l_strBmnname As String
        
Dim l_strUserid As String
        
Dim l_strTensouadd As String
        
Dim l_strRiyuu As String
        
Dim l_strBikou As String
        
Dim l_dtb As DataTable
        
Dim l_dtr As DataRow
        
Dim l_strColumnShainMei As DataColumn
        
Dim l_strColumnSinnkbn As DataColumn
        
Dim l_strColumnUserid As DataColumn
        
Dim l_strColumnShainNo As DataColumn
        
Dim l_strColumnBmncode As DataColumn
        
Dim l_strColumnBmnname As DataColumn
        
Dim l_strColumnHenkouShainNo As DataColumn
        
Dim l_strColumnHenkouShainName As DataColumn
        
Dim l_strColumnTensouadd As DataColumn
        
Dim l_strColumnRiyuu As DataColumn
        
Dim l_strColumnBikou As DataColumn
        
Dim l_blnFirst As Boolean = True

        l_objStreamReader 
= New System.IO.StreamReader(fleCVS.PostedFile.InputStream, Encoding.GetEncoding("shift-jis"))
        l_strFile 
= l_objStreamReader.ReadLine()

        
While Not (l_strFile Is Nothing)
            l_strFile 
= l_strFile.Replace(Convert.ToChar(9), ",")
            l_ArrayData 
= l_strFile.Split(","c)
            l_strShainNo 
= l_ArrayData(0)
            l_strShainMei 
= l_ArrayData(1)
            l_strBmncode 
= l_ArrayData(2)
            l_strBmnname 
= l_ArrayData(3)
            l_strHenkouShainNo 
= l_ArrayData(4)
            l_strHenkouShainName 
= l_ArrayData(5)
            l_strSinnkbn 
= l_ArrayData(6)
            l_strUserid 
= l_ArrayData(7)
            l_strTensouadd 
= l_ArrayData(8)
            l_strRiyuu 
= l_ArrayData(9)
            l_strBikou 
= l_ArrayData(10)

            
If l_blnFirst Then
                l_dtb 
= New DataTable

                l_strColumnSinnkbn 
= New DataColumn
                l_strColumnSinnkbn.ColumnName 
= "Sinnkbn"
                l_strColumnSinnkbn.DataType 
= GetType(System.String)
                l_strColumnSinnkbn.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnSinnkbn)

                l_strColumnShainNo 
= New DataColumn
                l_strColumnShainNo.ColumnName 
= "ShainNo"
                l_strColumnShainNo.DataType 
= GetType(System.String)
                l_strColumnShainNo.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnShainNo)

                l_strColumnShainMei 
= New DataColumn
                l_strColumnShainMei.ColumnName 
= "ShainMei"
                l_strColumnShainMei.DataType 
= GetType(System.String)
                l_strColumnShainMei.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnShainMei)

                l_strColumnUserid 
= New DataColumn
                l_strColumnUserid.ColumnName 
= "Userid"
                l_strColumnUserid.DataType 
= GetType(System.String)
                l_strColumnUserid.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnUserid)

                l_strColumnBmncode 
= New DataColumn
                l_strColumnBmncode.ColumnName 
= "Bmncode"
                l_strColumnBmncode.DataType 
= GetType(System.String)
                l_strColumnBmncode.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnBmncode)

                l_strColumnTensouadd 
= New DataColumn
                l_strColumnTensouadd.ColumnName 
= "Tensouadd"
                l_strColumnTensouadd.DataType 
= GetType(System.String)
                l_strColumnTensouadd.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnTensouadd)

                l_strColumnRiyuu 
= New DataColumn
                l_strColumnRiyuu.ColumnName 
= "Riyuu"
                l_strColumnRiyuu.DataType 
= GetType(System.String)
                l_strColumnRiyuu.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnRiyuu)

                l_strColumnBikou 
= New DataColumn
                l_strColumnBikou.ColumnName 
= "Bikou"
                l_strColumnBikou.DataType 
= GetType(System.String)
                l_strColumnBikou.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnBikou)

                l_strColumnBmnname 
= New DataColumn
                l_strColumnBmnname.ColumnName 
= "Bmnname"
                l_strColumnBmnname.DataType 
= GetType(System.String)
                l_strColumnBmnname.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnBmnname)

                l_strColumnHenkouShainNo 
= New DataColumn
                l_strColumnHenkouShainNo.ColumnName 
= "HenkouShainNo"
                l_strColumnHenkouShainNo.DataType 
= GetType(System.String)
                l_strColumnHenkouShainNo.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnHenkouShainNo)

                l_strColumnHenkouShainName 
= New DataColumn
                l_strColumnHenkouShainName.ColumnName 
= "HenkouShainName"
                l_strColumnHenkouShainName.DataType 
= GetType(System.String)
                l_strColumnHenkouShainName.AllowDBNull 
= False
                l_dtb.Columns.Add(l_strColumnHenkouShainName)

                l_blnFirst 
= False
            
End If

            l_dtr 
= l_dtb.NewRow()
            l_dtr(
"ShainNo"= l_strShainNo
            l_dtr(
"ShainMei"= l_strShainMei
            l_dtr(
"Bmncode"= l_strBmncode
            l_dtr(
"Bmnname"= l_strBmnname
            l_dtr(
"HenkouShainNo"= l_strHenkouShainNo
            l_dtr(
"HenkouShainName"= l_strHenkouShainName
            l_dtr(
"Sinnkbn"= l_strSinnkbn
            l_dtr(
"Userid"= l_strUserid
            l_dtr(
"Tensouadd"= l_strTensouadd
            l_dtr(
"Riyuu"= l_strRiyuu
            l_dtr(
"Bikou"= l_strBikou

            l_dtb.Rows.Add(l_dtr)
            l_strFile 
= l_objStreamReader.ReadLine()
        
End While
        l_objStreamReader.Close()
        Session(
"dtb"= l_dtb
        dgrData.DataSource 
= l_dtb
        dgrData.DataBind()
    
End Sub

Keyword: csv读取,Datagrid csv,  CSV,  csv导入Datagird,  Datagrid数据源
posted on 2007-08-18 22:29  Blue Sky  阅读(4098)  评论(0编辑  收藏  举报