■ CsvWriter 클래스의 WriteRecords 메소드를 사용해 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 41 42 43 44 45 46 47 48
|
using System.Globalization; using CsvHelper; using CsvHelper.Configuration; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<Student> sourceList = new List<Student> { new Student { ID = "ID004", Name = "김동수", Score = 80 } }; CsvConfiguration csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture); csvConfiguration.HasHeaderRecord = false; using(FileStream fileStream = File.Open("source.csv", FileMode.Append)) { using(StreamWriter streamWriter = new StreamWriter(fileStream)) { using(CsvWriter csvWriter = new CsvWriter(streamWriter, csvConfiguration)) { csvWriter.WriteRecords(sourceList); } } } } #endregion } |
TestProject.zip
■ 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 } |
TestProject.zip
■ CsvWriter 클래스의 WriteRecords 메소드를 사용해 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 41 42
|
using System.Globalization; using CsvHelper; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<Student> sourceList = new List<Student> { new Student { ID = "ID001", Name = "홍길동", Score = 90 }, new Student { ID = "ID002", Name = "김철수", Score = 100 }, new Student { ID = "ID003", Name = "이영희", Score = 80 } }; using(StreamWriter streamWriter = new StreamWriter("d:\\result.csv")) { using(CsvWriter csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture)) { csvWriter.WriteRecords(sourceList); } } } #endregion } |
TestProject.zip
■ CsvHelper 누겟을 설치하는 방법을 보여준다. 1. Visual Studio를 실행한다. 2. [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 실행한다.
더 읽기