简体   繁体   English

如何在C#.Net中获取JPEG文件编码UTF-8?

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

I wanna make CSV file encoding UTF-8. 我想制作UTF-8编码的CSV文件。 Now, my CSV file cannot show Japanese Fonts. 现在,我的CSV文件无法显示日文字体。 I want C# code to solve this problem. 我想要C#代码来解决这个问题。

SuSanda, 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. 如果您在Excel中打开该文件,它将按预期显示日语文本。
If you do not include the Encoding.UTF8 parameter, it will display gibberish. 如果您不包含Encoding.UTF8参数,它将显示乱码。

I hope that's what you're looking for. 我希望这就是你要找的东西。

This code helps to text from a csv file to save it as a encoded csv file. 此代码有助于从csv文件中将文本保存为编码的csv文件。 To use it Call as below and save it. 使用方法如下呼叫并保存。

GetCSVFileContent("Your_CSV_FileName") 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;
        }
 StringBuilder sb = new StringBuilder();          


 var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
 sb.AppendLine(string.Join(",", columnNames));

 foreach (DataRow row in dt.Rows)
 {
     var fields = row.ItemArray.Select(field => field.ToString()).ToArray();
     sb.AppendLine(string.Join(",", fields));
 }

 File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#.NET - 如何使用typeof()来继承? - C#.NET - How can I get typeof() to work with inheritance? 如何从URL中获取数据并将其保存到C#.NET中的二进制文件中,而不会出现编码问题? - How to GET data from an URL and save it into a file in binary in C#.NET without the encoding mess? 如何在C#.NET,PHP和MySQL之间获得通用编码? - How to get a common Encoding between C#.NET, PHP and MySQL? 如何在utf-8编码中读取XML文件 - How to read XML file in Encoding utf-8 如何使用C#.net中的SharpSVn DLL获取SVN工作文件夹中当前特定文件的先前版本? - How can I get the previous version of a particular file currently in the SVN working folder using SharpSVn DLL in C#.net? 如何在c#.net中搜索未注释的文本文件/ sql文件中的关键字? - How I can search keyword from text file/ sql file in c#.net which are not commented? 如何在 C#.net 中获取 manifest.json 文件? - How do i get manifest.json file in C#.net? 在C#.net中,我如何获取要发送到普通办公室打印机的文件内容 - In C#.net, How I get the Content of File Sent to Print to normal office printer 如何使用C#将html文本转换为utf-8 - How can I convert a html text to utf-8 with C# 如何在 C# 中将字符串转换为 UTF-8? - How can I transform string to UTF-8 in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM