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

后端 未结 3 1640
Happy的楠姐
Happy的楠姐 2021-02-12 19:51

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

3条回答
  •  心在旅途
    2021-02-12 20:48

    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;
            }
    

提交回复
热议问题