Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing French character in csv files in C#

Tags:

c#

encoding

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.

like image 214
Niraj Choubey Avatar asked Jan 23 '12 10:01

Niraj Choubey


People also ask

What encoding to use for French characters?

French Characters in HTML Documents - UTF-8 Encoding. This section provides a tutorial example on how enter and use French characters in HTML documents using Unicode UTF-8 encoding. The HTML document should include a meta tag with charset=utf-8 and be stored in UTF-8 format.

Can CSV have special characters?

Chances are, you will run into special characters within your data. So if you're working with CSV, what special characters are actually supported? Technically, all of them! There are no specified limits of what characters can be used in a CSV file.

Can CSV be Unicode?

Simple CSV files do not support Unicode/UTF-8 characters. This is a limitation of the CSV format and not something that can be changed in DEAR. However, it is possible to import/export Unicode characters following these steps. This article shows the process for Windows machines.

What encoding should I use for CSV?

It is recommended to always use UTF-8 encoding in your CSV files. Why? UTF-8 encoding contains 1,112,064 characters, which encompasses just about any character you would type, in any language.


2 Answers

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

Encoding.GetEncoding("iso-8859-1")
like image 189
Jaster Avatar answered Oct 25 '22 21:10

Jaster


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.

like image 42
Simon Mourier Avatar answered Oct 25 '22 20:10

Simon Mourier