Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get CSV file encoding UTF-8 in C#.Net?

Tags:

I wanna make CSV file encoding UTF-8. Now, my CSV file cannot show Japanese Fonts. I want C# code to solve this problem.

like image 639
SuSanda Avatar asked Dec 17 '10 03:12

SuSanda


People also ask

How do I know if a CSV file is UTF-8 encoded?

The evaluated encoding of the open file will display on the bottom bar, far right side. The encodings supported can be seen by going to Settings -> Preferences -> New Document/Default Directory and looking in the drop down.

What is CSV UTF-8 encoding?

UTF-8, or "Unicode Transformation Format, 8 Bit" is a marketing operations pro's best friend when it comes to data imports and exports. It refers to how a file's character data is encoded when moving files between systems.

What is the encoding of my CSV?

UTF-8 encoded CSV files will work well with Accompa whether they contain just English characters, or also contain non-English characters such as é, ç, ü.


2 Answers

SuSanda,
I'm not sure about your current code or your actual text you're trying to save, but this might get you in the right direction.

using(var sw = new StreamWriter("testfile_utf8.csv", false, Encoding.UTF8))
{
    sw.WriteLine("頼もう");
}

If you open that file in Excel, it will show the Japanese text as expected.
If you do not include the Encoding.UTF8 parameter, it will display gibberish.

I hope that's what you're looking for.

like image 146
BeemerGuy Avatar answered Sep 20 '22 18:09

BeemerGuy


This code helps to text from a csv file to save it as a encoded csv file. To use it Call as below and save it.

GetCSVFileContent("Your_CSV_FileName")

protected byte[] GetCSVFileContent(string fileName)
        {
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(fileName, Encoding.Default, true))
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
            }
            string allines = sb.ToString();


            UTF8Encoding utf8 = new UTF8Encoding();


            var preamble = utf8.GetPreamble();

            var data = utf8.GetBytes(allines);


            return data;
        }
like image 43
jAntoni Avatar answered Sep 23 '22 18:09

jAntoni