-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCsvHelper.cs
More file actions
47 lines (43 loc) · 1.45 KB
/
CsvHelper.cs
File metadata and controls
47 lines (43 loc) · 1.45 KB
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
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using CsvHelper;
using CsvHelper.Configuration;
namespace dtos_cohort_manager_e2e_tests.Helpers
{
public static class CsvHelperService
{
public static List<string> ExtractNhsNumbersFromCsv(string filePath)
{
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture));
var records = new List<string>();
csv.Read();
csv.ReadHeader();
while (csv.Read())
{
records.Add(csv.GetField(3));
}
return records;
}
public static List<Dictionary<string, string>> ReadCsv(string filePath)
{
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture));
var records = new List<Dictionary<string, string>>();
csv.Read();
csv.ReadHeader();
var headers = csv.HeaderRecord;
while (csv.Read())
{
var record = new Dictionary<string, string>();
foreach (var header in headers)
{
record[header] = csv.GetField(header);
}
records.Add(record);
}
return records;
}
}
}