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

时间:2010-12-17 03:01:16

标签: c# encoding csv utf-8

我想制作编码UTF-8的CSV文件。现在,我的CSV文件无法显示日文字体。我想要C#代码来解决这个问题。

3 个答案:

答案 0 :(得分:30)

SuSanda,
我不确定您当前的代码或您尝试保存的实际文本,但这可能会让您朝着正确的方向前进。

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

如果您在Excel中打开该文件,它将按预期显示日文文本 如果您不包含Encoding.UTF8参数,则会显示乱码。

我希望这就是你要找的东西。

答案 1 :(得分:4)

此代码有助于从csv文件发送文本以将其另存为编码的csv文件。 使用方法如下呼叫并保存。

  

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

答案 2 :(得分:1)

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