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

后端 未结 3 1620
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:36

     StringBuilder sb = new StringBuilder();          
    
    
     var columnNames = dt.Columns.Cast().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);
    

提交回复
热议问题