Writing CSV files using C#

gpeipman
58 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

CSV files with C#

This example introduces how to generate CSV files with C#. It uses NuGet package CsvHelper to accomplish a goal. CsvHelper is powerful but easy to use library that supports most important .NET Framework types. It is possible to write CSV-files with custom structure and it is also possible to register types and let library to convert them to CSV automatically.

Writing objects to CSV

Let's see first how to dump simple projects array to CSV. This is shortest I was able to invent to have CSV-file with headers row.

Click Run to see the demo
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
·using System;
using System.IO;
using System.Text;
using CsvHelper;
namespace CsvExample
{
public class Project
{
public string CustomerName { get; set; }
public string Title { get; set; }
public DateTime Deadline { get; set; }
}
public class Program
{
static void Main(string[] args)
{
var data = new[]
{
new Project { CustomerName = "Big Corp", Title = "CRM updates", Deadline = DateTime.Today.AddDays(-2) },
new Project { CustomerName = "Imaginary Corp", Title = "Sales system", Deadline = DateTime.Today.AddDays(1) }
};
using (var mem = new MemoryStream())
using (var writer = new StreamWriter(mem))
using (var csvWriter = new CsvWriter(writer))
{
csvWriter.Configuration.Delimiter = ";";
csvWriter.Configuration.HasHeaderRecord = true;
csvWriter.Configuration.AutoMap<Project>();
csvWriter.WriteHeader<Project>();
csvWriter.WriteRecords(data);
writer.Flush();
var result = Encoding.UTF8.GetString(mem.ToArray());
Console.WriteLine(result);
}
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Objects with fields

There is one special case - class with fields. Sometimes public fields are used instead of properties because property means two methods and one backing field. If there's no real need for properties then data migration applications often use public fields to keep objects smaller. This is something that CsvHelper does not support directly.

Click Run to see the demo
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
49
50
·using System;
using System.IO;
using System.Text;
using CsvHelper;
namespace CsvExample
{
public class Project
{
public string CustomerName;
public string Title;
public DateTime Deadline;
}
public class Program
{
static void Main(string[] args)
{
var data = new[]
{
new Project { CustomerName = "Big Corp", Title = "CRM updates", Deadline = DateTime.Today.AddDays(-2) },
new Project { CustomerName = "Imaginary Corp", Title = "Sales system", Deadline = DateTime.Today.AddDays(1) }
};
using (var mem = new MemoryStream())
using (var writer = new StreamWriter(mem))
using (var csvWriter = new CsvWriter(writer))
{
csvWriter.Configuration.Delimiter = ";";
csvWriter.WriteField("Customer");
csvWriter.WriteField("Title");
csvWriter.WriteField("Deadline");
csvWriter.NextRecord();
foreach (var project in data)
{
csvWriter.WriteField(project.CustomerName);
csvWriter.WriteField(project.Title);
csvWriter.WriteField(project.Deadline);
csvWriter.NextRecord();
}
writer.Flush();
var result = Encoding.UTF8.GetString(mem.ToArray());
Console.WriteLine(result);
}
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Both examples call Flush() method of StreamWriter because otherwise buffer is not filled when code creates CSV string.

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content