如何在vb.net中读取.csv excel格式文件 [英] how to read .csv excel format file in vb.net

查看:127
本文介绍了如何在vb.net中读取.csv excel格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在vb.net中读取.csv excel格式文件





公共函数GetCSVData()As System.Data.DataTable

Dim dtFormB As New System.Data.DataTable

Dim hasHeaders As Boolean = False

Dim HDR As String = If(hasHeaders,Yes ,否)

Dim strConn As String

Dim filePath As String



'filePath = g_strFileName +\+FormB.xls



filePath =D:\Temp \Khambhada \Khambhada_CSV.csv



如果filePath = Nothing那么

MessageBox.Show(请导入ROR表,层寄存器,MessageBoxButtons.OK,MessageBoxIcon.Information)

返回dtFormB

结束如果



如果是filePath.Substring(filePath.LastIndexOf(。c))。ToLower ()=。csv然后



strConn =Provider = Microsoft.Jet.OLEDB.4.0;数据源= D:\Temp \Khambhada \ Khambhada_CSV.csv;扩展属性=文本;高清R =是; FMT =定界





结束如果







Dim conn As New OleDbConnection(strConn)

conn.Open()

Dim schemaTable As System.Data.DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,New Object(){Nothing,Nothing,Nothing,TABLE})

'循环播放Xl文件总页数

'foreach(schemaTable.Rows中的DataRow schemaRow)

'{

'}



'循环第一张Xl文件

Dim schemaRow As DataRow = schemaTable.Rows(0)

Dim sheet As String = schemaRow(TABLE_NAME)。ToString()

如果不是sheet.EndsWith(_)那么

Dim query As String =SELECT * FROM [+ sheet +]

Dim daexcel As New OleDbDataAdapter(query,conn)

dtFormB.Locale = CultureInfo.CurrentCulture

daexcel.Fill(dtFormB)

结束If



对于i as Integer = 0到dtFormB.Rows.Count - 1



如果dtFormB.Rows(i)(1).ToString()。长度> 10然后

Dim strValue As String = dtFormB.Rows(i)(1).ToString()。Substring(0,10)

dtFormB.Rows(i)( 1)= strValue

结束如果



下一页



conn.Close ()

返回dtFormB

结束功能







嗨写了这个代码写的但是我收到错误的打开



请你尽快帮助我



i我得到的错误如下:

'D:\Temp \Khambhada \Khambhada_CSV.csv'不是有效路径。确保路径名拼写正确并且您已连接到文件所在的服务器。

解决方案

查看此链接希望它有帮助:)

点击这里


试试这个.. :)





如何在VB.Net中阅读csv文件内容 [ ^ ]



读取和写入Excel文件 [ ^ ]



VB.NET中的通用CSV文件处理 [ ^ ]

how to read .csv excel format file in vb.net


Public Function GetCSVData() As System.Data.DataTable
Dim dtFormB As New System.Data.DataTable
Dim hasHeaders As Boolean = False
Dim HDR As String = If(hasHeaders, "Yes", "No")
Dim strConn As String
Dim filePath As String

'filePath = g_strFileName + "\" + "FormB.xls"

filePath = "D:\Temp\Khambhada\Khambhada_CSV.csv"

If filePath = Nothing Then
MessageBox.Show("Please Import ROR Table", "Layer Register", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return dtFormB
End If

If filePath.Substring(filePath.LastIndexOf("."c)).ToLower() = ".csv" Then

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Temp\Khambhada\Khambhada_CSV.csv;Extended Properties=""text;HDR=Yes;FMT=Delimited"""


End If



Dim conn As New OleDbConnection(strConn)
conn.Open()
Dim schemaTable As System.Data.DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
'Looping Total Sheet of Xl File
'foreach (DataRow schemaRow in schemaTable.Rows)
' {
' }

'Looping a first Sheet of Xl File
Dim schemaRow As DataRow = schemaTable.Rows(0)
Dim sheet As String = schemaRow("TABLE_NAME").ToString()
If Not sheet.EndsWith("_") Then
Dim query As String = "SELECT * FROM [" + sheet + "]"
Dim daexcel As New OleDbDataAdapter(query, conn)
dtFormB.Locale = CultureInfo.CurrentCulture
daexcel.Fill(dtFormB)
End If

For i As Integer = 0 To dtFormB.Rows.Count - 1

If dtFormB.Rows(i)(1).ToString().Length > 10 Then
Dim strValue As String = dtFormB.Rows(i)(1).ToString().Substring(0, 10)
dtFormB.Rows(i)(1) = strValue
End If

Next

conn.Close()
Return dtFormB
End Function



Hi written this code for written but i am getting error on con.open

please can u help me asap

i am getting this error like :
'D:\Temp\Khambhada\Khambhada_CSV.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

解决方案

Check out this link hope it helps :)
Click Here


try this.. :)


How to read csv file contents in VB.Net[^]

Read and Write Excel Files[^]

General Purpose CSV File Handling in VB.NET[^]


这篇关于如何在vb.net中读取.csv excel格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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