以下是使用 C# 和 CsvHelper 库将 CSV 文件转换为 UTF-8 格式的示例代码:

using System;
using System.IO;
using System.Text;
using CsvHelper;

namespace CsvReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 读取 CSV 文件
            using (var reader = new StreamReader('data.csv'))
            using (var csv = new CsvReader(reader))
            {
                // 将编码格式转换为 UTF-8
                csv.Configuration.Encoding = Encoding.UTF8;

                // 读取 CSV 数据并输出
                foreach (var record in csv.GetRecords<dynamic>())
                {
                    Console.WriteLine($'{record.Id}, {record.Name}');
                }
            }
        }
    }
}

在上面的代码中,我们通过 StreamReader 读取 CSV 文件,然后使用 CsvReader 将其解析为记录。然后,我们将 Encoding 属性设置为 Encoding.UTF8,以确保输出为 UTF-8 格式。最后,我们使用 GetRecords<dynamic>() 方法读取记录并输出。

C# 使用 CsvHelper 将 CSV 文件转换为 UTF-8 格式

原文地址: https://www.cveoy.top/t/topic/oRjo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录