1

I am importing data from a csv file into my vb.net application. Below is the current code I am using to achieve this:

 Dim myImportConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & txtboxSelectFile.Text & "';Extended Properties=""Excel 8.0;IMEX=1""" Dim MyImportConnection As New System.Data.OleDb.OleDbConnection MyImportConnection.ConnectionString = myImportConnectionString MyImportConnection.Open() Dim myImportAdapterString As String = "select * from [" & mySheet & "$]" Dim MyImportCommand As New System.Data.OleDb.OleDbCommand(myImportAdapterString, MyImportConnection) Dim myImportDataAdapter As New System.Data.OleDb.OleDbDataAdapter(MyImportCommand) Dim myImportDataTable As New System.Data.DataTable myImportDataAdapter.Fill(myImportDataTable) dgvFirst.DataSource = myImportDataTable myImportDataAdapter.Dispose() MyImportCommand.Dispose() MyImportConnection.Close() MyImportConnection.Dispose() GC.Collect() 

The problem I have is that the data is UTF8 encoded. When I import it, any special characters are being displayed incorrectly. The only way I can open the csv file and use the correct encoding is by opening it manually, using Excel, and importing the data to a new Excel file.

Is there any way I can specify the encoding to use when the file is being automatically imported into a vb.net datatable so that foreign characters are displayed correctly?

Many Thanks :)

3
  • 2
    If you are trying to read a CSV file, isn't what you are doing a bit overkill? Commented Aug 3, 2016 at 8:31
  • Have a look at this Commented Aug 3, 2016 at 8:32
  • 1
    Why are you using OLEDB to query a csv file!?? Commented Aug 3, 2016 at 10:26

1 Answer 1

0

You can add ;CharacterSet=ANSI as input encoding to the Extended Properties:

Replace

Dim myImportConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='"_ & txtboxSelectFile.Text & "';Extended Properties=""Excel 8.0;IMEX=1""" 

by

Dim myImportConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='"_ & txtboxSelectFile.Text & "';Extended Properties=""Excel 8.0;IMEX=1"";CharacterSet=ANSI" 

(idea comes from here)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.