Click here to Skip to main content
16,009,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 15-Apr-14 23:53pm
v2

Check out this link hope it helps :)
Click Here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900