LoginSignup
1
2

More than 5 years have passed since last update.

【C#】CsvHelperでCSVをIEnumerable<T>オブジェクトに変換

Last updated at Posted at 2016-05-22
CodePage Name DisplayName Note
932 shift_jis Japanese (Shift-JIS)
20932 EUC-JP Japanese (JIS 0208-1990 and 0212-1990)
65001 utf-8 Unicode (UTF-8)

上記テーブルを表すCSVファイルをencodings.csvという名前でCドライブ直下に保存した場合、下記コードでNameフィールドの値の一覧を表示することができる。

using CsvHelper;
using System;
using System.IO;

class EncodingInfo
{
    public int CodePage { get; set; }
    public string Name { get; set; }
    public string DisplayName { get; set; }
}

class Sample
{
    public static void Main(string[] args)
    {
        var path = @"C:\encodings.csv";
        using (var reader = new StreamReader(path))
        {
            var csv = new CsvReader(reader);
            var records = csv.GetRecords<EncodingInfo>();
            foreach (var info in records)
            {
                Console.WriteLine(info.Name);
            }
        }
    }
}
  • 数値型プロパティのフィールドに英字を含めると、CsvHelper.TypeConversion.CsvTypeConverterException例外が発生する。

https://github.com/JoshClose/CsvHelper
https://joshclose.github.io/CsvHelper/

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2