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.