7

I have a dbf file which has MONTRÉAL as one of the entires. I have to convert this dbf file into a csv file.

For this I am reading this dbf file using Microsoft Visual FoxPro Driver driver, then converting converting each rows to comma separated, and then writing all lines into a file named "ASD.csv".

But in CSV file MONTRÉAL is displayed as MONTR?AL i.e in place of É a ? character appears. I tried using UTF8 encoding while writing the rows to csv file but even this did not work.

Please help.

ANeves
  • 6,219
  • 3
  • 39
  • 63
Niraj Choubey
  • 3,942
  • 18
  • 58
  • 93
  • What encoding is the file you are reading and are you reading it in that encoding? – Oskar Kjellin Jan 23 '12 at 10:21
  • What application are you using to view the CSV file? What encoding does it use? Possibly the CSV file is correct, but the viewer uses the wrong encoding. (There is no real CSV standard, and no preferred encoding for CSV files, and the file doesn't record the used encoding either. It has to be agreed between the producing and consuming application seperately.) – Codo Jan 23 '12 at 10:22
  • I am using Notepad++ to view CSV file. – Niraj Choubey Jan 23 '12 at 10:27
  • What encoding do you have selected in Notepad++ (menu _Encoding_)? Is it the same you used for the export? – Codo Jan 23 '12 at 10:30
  • I used the answer given by Tural in http://stackoverflow.com/questions/3845760/encoding-problems-with-dbase-iii-dbf-files-on-different-machines and it worked. – Niraj Choubey Jan 23 '12 at 11:19

3 Answers3

7

you have to use a specific encoding. for french it is "iso-8859-1" or "windows-1252".

Encoding.GetEncoding("iso-8859-1")
Jaster
  • 8,255
  • 3
  • 34
  • 60
1

My implementation a few years ago used:

var writer = new StreamWriter(fileStream, Encoding.Unicode);
writer.Write(csvContent.ToCharArray());

I guess that should work since quite some languages were supported in our application, including Japanese.

edit: Works for MONTRÉAL with Notepad, Notepad++, Excel, LibreOffice ...

And a good explanation of UTF8, Unicode and other encodings differences.

Community
  • 1
  • 1
doblak
  • 3,036
  • 1
  • 27
  • 22
1

I suggest you use UTF8 encoding like this:

    using (StreamWriter writer = new StreamWriter("asd.csv", false, Encoding.UTF8))
    {
        writer.WriteLine("Id;City");
        writer.WriteLine("1;Montréal");
        writer.WriteLine("2;Québec");
    }

This will add a BOM (Byte Order Mark) to the beginning of the CSV and you can then open the file with Excel directly, or even with the standard Windows Notepad successfully.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298