VB.net中读取CSV到DataGrid

本文介绍了一种将CSV文件的数据导入到DataGrid的方法,并提供了一段详细的VB.NET代码示例。该方法首先读取CSV文件的内容,然后创建一个DataTable来存储这些数据,最后将数据绑定到DataGrid进行展示。
摘要由CSDN通过智能技术生成
  新技术的发展有点应接不暇,用户的胃口则被吊得越来越高,软件开发的质量也越来越高,因此对程序员的代码质量也随之变高。回到正题,今天我要说的是有关读取CSV文件到DataGrid表中,因为我在好几个项目中都用到了,页面很简单。



    
 下面是实现代码:
ExpandedBlockStart.gif ContractedBlock.gif      Private   Sub btnFileLoad_Click() Sub btnFileLoad_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnFileLoad.Click
InBlock.gif        
Dim l_objStreamReader As System.IO.StreamReader
InBlock.gif        
Dim l_ArrayData() As String
InBlock.gif        
Dim l_strFile As String
InBlock.gif        
Dim l_strSinnkbn As String
InBlock.gif        
Dim l_strHenkouShainNo As String
InBlock.gif        
Dim l_strHenkouShainName As String
InBlock.gif        
Dim l_strShainNo As String
InBlock.gif        
Dim l_strShainMei As String
InBlock.gif        
Dim l_strBmncode As String
InBlock.gif        
Dim l_strBmnname As String
InBlock.gif        
Dim l_strUserid As String
InBlock.gif        
Dim l_strTensouadd As String
InBlock.gif        
Dim l_strRiyuu As String
InBlock.gif        
Dim l_strBikou As String
InBlock.gif        
Dim l_dtb As DataTable
InBlock.gif        
Dim l_dtr As DataRow
InBlock.gif        
Dim l_strColumnShainMei As DataColumn
InBlock.gif        
Dim l_strColumnSinnkbn As DataColumn
InBlock.gif        
Dim l_strColumnUserid As DataColumn
InBlock.gif        
Dim l_strColumnShainNo As DataColumn
InBlock.gif        
Dim l_strColumnBmncode As DataColumn
InBlock.gif        
Dim l_strColumnBmnname As DataColumn
InBlock.gif        
Dim l_strColumnHenkouShainNo As DataColumn
InBlock.gif        
Dim l_strColumnHenkouShainName As DataColumn
InBlock.gif        
Dim l_strColumnTensouadd As DataColumn
InBlock.gif        
Dim l_strColumnRiyuu As DataColumn
InBlock.gif        
Dim l_strColumnBikou As DataColumn
InBlock.gif        
Dim l_blnFirst As Boolean = True
InBlock.gif
InBlock.gif        l_objStreamReader 
= New System.IO.StreamReader(fleCVS.PostedFile.InputStream, Encoding.GetEncoding("shift-jis"))
InBlock.gif        l_strFile 
= l_objStreamReader.ReadLine()
InBlock.gif
InBlock.gif        
While Not (l_strFile Is Nothing)
InBlock.gif            l_strFile 
= l_strFile.Replace(Convert.ToChar(9), ",")
InBlock.gif            l_ArrayData 
= l_strFile.Split(","c)
InBlock.gif            l_strShainNo 
= l_ArrayData(0)
InBlock.gif            l_strShainMei 
= l_ArrayData(1)
InBlock.gif            l_strBmncode 
= l_ArrayData(2)
InBlock.gif            l_strBmnname 
= l_ArrayData(3)
InBlock.gif            l_strHenkouShainNo 
= l_ArrayData(4)
InBlock.gif            l_strHenkouShainName 
= l_ArrayData(5)
InBlock.gif            l_strSinnkbn 
= l_ArrayData(6)
InBlock.gif            l_strUserid 
= l_ArrayData(7)
InBlock.gif            l_strTensouadd 
= l_ArrayData(8)
InBlock.gif            l_strRiyuu 
= l_ArrayData(9)
InBlock.gif            l_strBikou 
= l_ArrayData(10)
InBlock.gif
InBlock.gif            
If l_blnFirst Then
InBlock.gif                l_dtb 
= New DataTable
InBlock.gif
InBlock.gif                l_strColumnSinnkbn 
= New DataColumn
InBlock.gif                l_strColumnSinnkbn.ColumnName 
= "Sinnkbn"
InBlock.gif                l_strColumnSinnkbn.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnSinnkbn.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnSinnkbn)
InBlock.gif
InBlock.gif                l_strColumnShainNo 
= New DataColumn
InBlock.gif                l_strColumnShainNo.ColumnName 
= "ShainNo"
InBlock.gif                l_strColumnShainNo.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnShainNo.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnShainNo)
InBlock.gif
InBlock.gif                l_strColumnShainMei 
= New DataColumn
InBlock.gif                l_strColumnShainMei.ColumnName 
= "ShainMei"
InBlock.gif                l_strColumnShainMei.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnShainMei.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnShainMei)
InBlock.gif
InBlock.gif                l_strColumnUserid 
= New DataColumn
InBlock.gif                l_strColumnUserid.ColumnName 
= "Userid"
InBlock.gif                l_strColumnUserid.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnUserid.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnUserid)
InBlock.gif
InBlock.gif                l_strColumnBmncode 
= New DataColumn
InBlock.gif                l_strColumnBmncode.ColumnName 
= "Bmncode"
InBlock.gif                l_strColumnBmncode.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnBmncode.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnBmncode)
InBlock.gif
InBlock.gif                l_strColumnTensouadd 
= New DataColumn
InBlock.gif                l_strColumnTensouadd.ColumnName 
= "Tensouadd"
InBlock.gif                l_strColumnTensouadd.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnTensouadd.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnTensouadd)
InBlock.gif
InBlock.gif                l_strColumnRiyuu 
= New DataColumn
InBlock.gif                l_strColumnRiyuu.ColumnName 
= "Riyuu"
InBlock.gif                l_strColumnRiyuu.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnRiyuu.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnRiyuu)
InBlock.gif
InBlock.gif                l_strColumnBikou 
= New DataColumn
InBlock.gif                l_strColumnBikou.ColumnName 
= "Bikou"
InBlock.gif                l_strColumnBikou.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnBikou.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnBikou)
InBlock.gif
InBlock.gif                l_strColumnBmnname 
= New DataColumn
InBlock.gif                l_strColumnBmnname.ColumnName 
= "Bmnname"
InBlock.gif                l_strColumnBmnname.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnBmnname.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnBmnname)
InBlock.gif
InBlock.gif                l_strColumnHenkouShainNo 
= New DataColumn
InBlock.gif                l_strColumnHenkouShainNo.ColumnName 
= "HenkouShainNo"
InBlock.gif                l_strColumnHenkouShainNo.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnHenkouShainNo.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnHenkouShainNo)
InBlock.gif
InBlock.gif                l_strColumnHenkouShainName 
= New DataColumn
InBlock.gif                l_strColumnHenkouShainName.ColumnName 
= "HenkouShainName"
InBlock.gif                l_strColumnHenkouShainName.DataType 
= GetType(System.String)
InBlock.gif                l_strColumnHenkouShainName.AllowDBNull 
= False
InBlock.gif                l_dtb.Columns.Add(l_strColumnHenkouShainName)
InBlock.gif
InBlock.gif                l_blnFirst 
= False
InBlock.gif            
End If
InBlock.gif
InBlock.gif            l_dtr 
= l_dtb.NewRow()
InBlock.gif            l_dtr(
"ShainNo"= l_strShainNo
InBlock.gif            l_dtr(
"ShainMei"= l_strShainMei
InBlock.gif            l_dtr(
"Bmncode"= l_strBmncode
InBlock.gif            l_dtr(
"Bmnname"= l_strBmnname
InBlock.gif            l_dtr(
"HenkouShainNo"= l_strHenkouShainNo
InBlock.gif            l_dtr(
"HenkouShainName"= l_strHenkouShainName
InBlock.gif            l_dtr(
"Sinnkbn"= l_strSinnkbn
InBlock.gif            l_dtr(
"Userid"= l_strUserid
InBlock.gif            l_dtr(
"Tensouadd"= l_strTensouadd
InBlock.gif            l_dtr(
"Riyuu"= l_strRiyuu
InBlock.gif            l_dtr(
"Bikou"= l_strBikou
InBlock.gif
InBlock.gif            l_dtb.Rows.Add(l_dtr)
InBlock.gif            l_strFile 
= l_objStreamReader.ReadLine()
InBlock.gif        
End While
InBlock.gif        l_objStreamReader.Close()
InBlock.gif        Session(
"dtb"= l_dtb
InBlock.gif        dgrData.DataSource 
= l_dtb
InBlock.gif        dgrData.DataBind()
ExpandedBlockEnd.gif    
End Sub

Keyword: csv读取,Datagrid csv,  CSV,  csv导入Datagird,  Datagrid数据源

转载于:https://www.cnblogs.com/Qizai/archive/2007/08/18/861244.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值