Skip to content

Commit 302796d

Browse files
committed
Add CsvHelperExercise
1 parent 4311ecc commit 302796d

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="CsvHelper" Version="33.0.1" />
12+
</ItemGroup>
13+
14+
</Project>

CsvHelperExercise/Program.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using CsvHelper;
2+
using System.Globalization;
3+
4+
namespace CsvHelperExercise
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var students = new List<StudentInfo>
11+
{
12+
new StudentInfo { ID = 1, Name = "张三", Age = 20, Class = "终极一班", Gender = "男", Address = "北京市东城区" },
13+
new StudentInfo { ID = 2, Name = "李四", Age = 21, Class = "终极一班", Gender = "女", Address = "上海市黄浦区" },
14+
new StudentInfo { ID = 3, Name = "王五", Age = 22, Class = "终极一班", Gender = "男", Address = "广州市越秀区" },
15+
new StudentInfo { ID = 4, Name = "赵六", Age = 20, Class = "终极二班", Gender = "女", Address = "深圳市福田区" },
16+
new StudentInfo { ID = 5, Name = "孙七", Age = 23, Class = "终极二班", Gender = "男", Address = "杭州市西湖区" },
17+
new StudentInfo { ID = 6, Name = "周八", Age = 24, Class = "终极二班", Gender = "女", Address = "南京市玄武区" },
18+
new StudentInfo { ID = 7, Name = "吴九", Age = 22, Class = "终极二班", Gender = "男", Address = "成都市锦江区" },
19+
new StudentInfo { ID = 8, Name = "小袁", Age = 21, Class = "终极三班", Gender = "女", Address = "重庆市渝中区" },
20+
new StudentInfo { ID = 9, Name = "大姚", Age = 20, Class = "终极三班", Gender = "男", Address = "武汉市武昌区" },
21+
new StudentInfo { ID = 10, Name = "追逐时光者", Age = 23, Class = "终极三班", Gender = "女", Address = "长沙市天心区" }
22+
};
23+
24+
//写入CSV文件数据
25+
using var writer = new StreamWriter(@".\StudentInfoFile.csv");
26+
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
27+
csvWriter.WriteRecords(students);
28+
29+
//读取CSV文件数据
30+
using var reader = new StreamReader(@".\StudentInfoFile.csv");
31+
using var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
32+
var getStudentInfos = csvReader.GetRecords<StudentInfo>().ToList();
33+
}
34+
}
35+
}

CsvHelperExercise/StudentInfo.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsvHelperExercise
8+
{
9+
public class StudentInfo
10+
{
11+
/// <summary>
12+
/// 学生学号
13+
/// </summary>
14+
public int ID { get; set; }
15+
16+
/// <summary>
17+
/// 学生姓名
18+
/// </summary>
19+
public string Name { get; set; }
20+
21+
/// <summary>
22+
/// 学生年龄
23+
/// </summary>
24+
public int Age { get; set; }
25+
26+
/// <summary>
27+
/// 班级
28+
/// </summary>
29+
public string Class { get; set; }
30+
31+
/// <summary>
32+
/// 性别
33+
/// </summary>
34+
public string Gender { get; set; }
35+
36+
/// <summary>
37+
/// 住址
38+
/// </summary>
39+
public string Address { get; set; }
40+
}
41+
}

DotNetExercises.sln

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpectreExercise", "SpectreE
2525
EndProject
2626
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotnetSpiderExercise", "DotnetSpiderExercise\DotnetSpiderExercise.csproj", "{DA7E80D3-B6B2-4183-A50F-1EF63978D9E4}"
2727
EndProject
28-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileCompDecompExercise", "FileCompDecompExercise\FileCompDecompExercise.csproj", "{6F0DF339-E77F-4856-99E9-6F0BB77B3402}"
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileCompDecompExercise", "FileCompDecompExercise\FileCompDecompExercise.csproj", "{6F0DF339-E77F-4856-99E9-6F0BB77B3402}"
2929
EndProject
30-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapsuiExercise", "MapsuiExercise\MapsuiExercise.csproj", "{A28271B3-3DAF-43F1-A925-1B9F3A9969D1}"
30+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapsuiExercise", "MapsuiExercise\MapsuiExercise.csproj", "{A28271B3-3DAF-43F1-A925-1B9F3A9969D1}"
31+
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CsvHelperExercise", "CsvHelperExercise\CsvHelperExercise.csproj", "{7B4C6757-3789-4F9A-AC97-A82170330A90}"
3133
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -87,6 +89,10 @@ Global
8789
{A28271B3-3DAF-43F1-A925-1B9F3A9969D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
8890
{A28271B3-3DAF-43F1-A925-1B9F3A9969D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
8991
{A28271B3-3DAF-43F1-A925-1B9F3A9969D1}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{7B4C6757-3789-4F9A-AC97-A82170330A90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{7B4C6757-3789-4F9A-AC97-A82170330A90}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{7B4C6757-3789-4F9A-AC97-A82170330A90}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{7B4C6757-3789-4F9A-AC97-A82170330A90}.Release|Any CPU.Build.0 = Release|Any CPU
9096
EndGlobalSection
9197
GlobalSection(SolutionProperties) = preSolution
9298
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)