-
-
Notifications
You must be signed in to change notification settings - Fork 0
CsvCoreWriter
The CsvCoreWriter lets you write CSV files using a strongly typed approach, supports header mapping and even setting a custom delimiter.
There are two ways to use the CsvCoreWriter:
-
Constructor Injection: You can use the
CsvCoreWriterdirectly by passing theICsvCoreWriterinterface as parameter into the constructor. - Initialize: Create an instance of the writer and use this (not preffered!).
Csv files are based on a delimiter, normally you can use the system settings for the delimiter to write.
In case you need to use a different delimiter, you can use the UseDelimiter method to set a custom delimiter.
| Description | |
|---|---|
| Parameters | char |
| Returns | void |
| Usage | csvCoreWriter.UseDelimiter() |
Example: Using a custom delimiter
var records = new List<ResultModel>
{
new()
{
Name = "Foo",
Surname = "Bar",
BirthDate = new DateOnly(2025, 04, 16),
Email = "foo@bar.nl"
}
};
csvWriter
.UseDelimiter('|')
.Write(Path.Combine("AnyPath", "YourFile.csv"), records);
When you want to write a CSV file without a header record, you should use the WithoutHeader method on the writer.
| Description | |
|---|---|
| Parameters | |
| Returns | void |
| Usage | csvCoreReader.WithoutHeader() |
Example: Writing without a header record
csharp
var records = new List<ResultModel>
{
new()
{
Name = "Foo",
Surname = "Bar",
BirthDate = new DateOnly(2025, 04, 16),
Email = "foo@bar.nl"
}
};
csvWriter
.WithoutHeader()
.Write(Path.Combine("AnyPath", "YourFile.csv"), records);
csv - output
Foo;Bar;01/01/2025;foo@bar.com
The most basic use of the writer is to use constructor injection, use the field to call the Write method. When you call the Write method, you need to pass the full path to the csv file and a list of your model.
As told earlier, by default we won't validate your data, see the validation chapter in this wiki. If you have specified a non-nullable dateonly / datetime property in your model, and the csv file contains a null value for that property, the reader will set these properties to their MinValues.
| Description | |
|---|---|
| Parameters | string the full path to the csv. List<T> the records you want to be in the csv file |
| Returns | void |
| Usage | csvCoreWriter.Write(Path.Combine("AnyPath", "YourFile.csv"), new List\<T\>()) |
| Async Usage | await csvCoreWriter.WriteAsync(Path.Combine("AnyPath", "YourFile.csv"), new List\<T\>()) |
Example: Writes the input models to the csv file
csharp
var records = new List<ResultModel>
{
new()
{
Name = "Foo",
Surname = "Bar",
BirthDate = new DateOnly(2025, 04, 16),
Email = "foo@bar.nl"
}
};
csvWriter
.Write(Path.Combine("AnyPath", "YourFile.csv"), records);
Example: Writes the input models asynchronized to the csv file
csharp
var records = new List<ResultModel>
{
new()
{
Name = "Foo",
Surname = "Bar",
BirthDate = new DateOnly(2025, 04, 16),
Email = "foo@bar.nl"
}
};
await csvWriter
.WriteAsync(Path.Combine("AnyPath", "YourFile.csv"), records);
csv - output
Name;Surname;Birthdate;Email
Foo;Bar;01/01/2025;foo@bar.com
If you create a model, you can use the properties in your model to map the column names in you csv file.
By default, the writer will use the property names in your model to create the header record in the csv file.
If you want to use a different name for the header record, you can use the [Header] attribute.
Example: Model that contains different property names
csharp
public class CsvCustomHeaderModel
{
[Header(0, "Firstname")]
public string Name { get; set; }
[Header(1, "family_name")]
public string Surname { get; set; }
[Header(2, "dateOfBirth")]
public DateOnly BirthDate { get; set; }
[Header(3, "email contact")]
public string Email { get; set; }
}
csv - output
Firstname;family_name;dateOfBirth;email contact
Foo;Bar;01/01/2025;foo@bar.com