将CSV导入DataTable VB.Net [英] Importing CSV to DataTable VB.Net

查看:111
本文介绍了将CSV导入DataTable VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以读取csv文件,然后将内容复制到DataTable。我已经创建了一个文件对话框,因此我可以选择要导入的电子表格,但是无法确定如何执行此操作。文件位置和名称存储在只读文本框中。



我尝试将[Apr2013.csv]更改为[& txtImportSpreadsheet.text& 等,并且也尝试使用变量文件夹。



  Dim  folder =   N:\ access \Telephones \ 
< span class =code-keyword> Dim con = Provider = Microsoft.Jet.OLEDB.4.0 ;数据来源=&文件夹& ;扩展属性=文本; HDR =否; FMT =定界;
Dim dt 作为 DataTable

使用 Adp 作为 OleDbDataAdapter( Select * From [Apr2013.csv],con)
Adp.Fill(dt)
结束 使用





如果有人能帮助我,我真的很感激。

解决方案

也尝试String.Format(这个文本添加名为:[{0}],filename的文件



其中filename是一个字符串变量,添加到主字符串中。是的



LG out


解决方案1有问题。循环逻辑不起作用,因为在最后一个有效行上,字符串不为空,因此它会再次循环,但会立即清空字符串,使其在后一个分割阶段失败。



另外,他调暗我很长但不使用它。



试试这个



 Dim SR As StreamReader = New StreamReader(your csv file path)
Dim line As String = SR.ReadLine()
Dim strArray As String() = line.Split(,c)
Dim dt As DataTable = New DataTable()
Dim row As DataRow

For each s As String in strArray
dt.Columns.Add(New DataColumn())
Next

Do
line = SR.ReadLine
If Not line = String.Empty Then
row = dt.NewRow()
row.ItemArray = line.Split(,c)
dt.Rows.Add(row)
Else
退出Do
结束如果
Loop


  Dim  SR  As  StreamReader =  StreamReader(  1.txt
Dim i As = 0
昏暗 line As String = SR.ReadLine()
Dim strArray As String ()= line.Split(
Dim dt As DataTable = DataTable()
Dim row As DataRow

对于 每个 s 作为 字符串 strArray
dt.Columns.Add( DataColumn ())
下一步
执行
line = SR.ReadLine
row = dt.NewRow()
row.ItemArray = line.Split(
dt.Rows.Add(row)


循环 while line = 字符串。空







在datatable dt中你将获得所有价值....! / BLOCKQUOTE>

I have some code that reads csv files and then copies the contents to a DataTable. I have created a file dialog so I can select the spreadsheet that I wish to import, however am having trouble figuring out how to do this. The file location and name are stored in a read only text box.

I have tried changing the [Apr2013.csv] to ["& txtImportSpreadsheet.text &"] ect and have also tried the same with the variable folder.

Dim folder = "N:\access\Telephones\"
Dim con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
Dim dt As New DataTable

Using Adp As New OleDbDataAdapter("Select * From [Apr2013.csv]", con)
Adp.Fill(dt)
End Using



If anyone could help me out I would really be grateful.

解决方案

Also try String.Format("This text to add a file named: [{0}]", filename)

where filename is a string variable, to added into the main string. Yay

LG out


Solution 1 is faulty. The loop logic doesn't work because on the last valid line, the string isn't empty so it loops again but then immediately empties the string making it fail at the latter split stage.

Also, he dims "i As Long" but doesn't use it.

Try this

Dim SR As StreamReader = New StreamReader("your csv file path")
        Dim line As String = SR.ReadLine()
        Dim strArray As String() = line.Split(","c)
        Dim dt As DataTable = New DataTable()
        Dim row As DataRow

        For Each s As String In strArray
            dt.Columns.Add(New DataColumn())
        Next

        Do
            line = SR.ReadLine
            If Not line = String.Empty Then
                row = dt.NewRow()
                row.ItemArray = line.Split(","c)
                dt.Rows.Add(row)
            Else
                Exit Do
            End If
        Loop


Dim SR As StreamReader = New StreamReader("1.txt")
               Dim i As Long = 0
               Dim line As String = SR.ReadLine()
               Dim strArray As String() = line.Split(",")
               Dim dt As DataTable = New DataTable()
               Dim row As DataRow

               For Each s As String In strArray
                   dt.Columns.Add(New DataColumn())
               Next
               Do
                   line = SR.ReadLine
                   row = dt.NewRow()
                   row.ItemArray = line.Split(",")
                   dt.Rows.Add(row)


               Loop While Not line = String.Empty




In datatable dt you will have all values....!


这篇关于将CSV导入DataTable VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆