Greetings helpful delightful people,
I have a problem with reading CSV files and converting to datatables in VB.Net.
If the CSV file contains a column full of '-' then on import into the datatable they appear as '0' and the entire column is formatted in a numeric format.
The code I have written is:
Public Function LoadCsvFile(filePath As String) As DataTable
Try
DTReadCSV = New DataTable
Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Microsoft.VisualBasic.Left(filePath, InStrRev(filePath, "")) & ";Extended Properties=""text;HDR=Yes;FMT=Delimited""")
Dim adapter As New OleDb.OleDbDataAdapter("SELECT * FROM [" + Microsoft.VisualBasic.Mid(filePath, InStrRev(filePath, "") + 1) + "]", connection)
'Dim table As New DataTable()
adapter.Fill(DTReadCSV)
'now thats its nicely in a datatable
IntAmountRows = DTReadCSV.Rows.Count
IntAmountColumns = DTReadCSV.Columns.Count
'System.Diagnostics.Debug.Print(DTReadCSV.Rows.Item(1)(1).ToString)
Return DTReadCSV
Exit Function
Catch ex As Exception
MsgBox(ex.ToString())
MsgBox(Err.Number & " " & Chr(13) & Err.Description)
End Try
End Function
Please can someone smarter figure out how to combat this issue besides modifying the CSV file by taking out the '-' as blanks, which at the moment seems the only long winded way of importing these CSV files.
Many thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…