■ CsvReader 클래스의 GetRecords 메소드를 사용해 CSV 파일을 로드하는 방법을 보여준다.
▶ Student.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using CsvHelper.Configuration.Attributes; namespace TestProject; /// <summary> /// 학생 /// </summary> public class Student { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> [Name("ID")] public string ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> [Name("성명")] public string Name { get; set; } #endregion #region 점수 - Score /// <summary> /// 점수 /// </summary> [Name("점수")] public int Score { get; set; } #endregion } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System.Globalization; using CsvHelper; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(StreamReader streamReader = new StreamReader("source.csv")) { using(CsvReader csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture)) { List<Student> resultList = csvReader.GetRecords<Student>().ToList(); foreach(Student student in resultList) { Console.WriteLine($"{student.ID}, {student.Name}, {student.Score}"); } } } } #endregion } |