NuGet package that provides useful file methods. Specifically, CSV file reading and writing.
$ dotnet add package Sion.Useful.FilesNuGet package that provides useful file methods. Specifically, CSV file reading and writing.
public class OutputFormatAttribute : Attribute
public static IEnumerable<string[]> Read(string path, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null)
public static IEnumerable<RowType> Read<RowType>(string path, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null) where RowType : class
public static IEnumerable<RowType> Read<RowType>(string path, Func<string[], RowType> customMappingFunc, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null)
public static async Task<IEnumerable<string[]>> ReadAsync(string path, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null)
public static async Task<IEnumerable<RowType>> ReadAsync<RowType>(string path, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null) where RowType : class
public static async Task<IEnumerable<RowType>> ReadAsync<RowType>(string path, Func<string[], RowType> customMappingFunc, string delimiter = ",", bool hasHeader = false, Encoding? encoding = null)
public static void Write(IEnumerable<IEnumerable<string>> rows, string path, string delimiter = ",", Encoding? encoding = null)
public static void Write<RowType>(IEnumerable<RowType> rows, string path, bool writeHeader, string delimiter = ",", Encoding? encoding = null) where RowType : class
public static async Task WriteAsync(IEnumerable<IEnumerable<string>> rows, string path, string delimiter = ",", Encoding? encoding = null)
public static async Task WriteAsync<RowType>(IEnumerable<RowType> rows, string path, bool writeHeader, string delimiter = ",", Encoding? encoding = null) where RowType : class
First, download the Sion.Useful.Files NuGet package, then include this using statement at the top of the file:
using Sion.Useful.Files;
Things to note:
// Every row in our CSV file is a Student object
public class Student {
public long Id { get; set; }
public string FirstName { get; set; }
public string? MiddleName { get; set; }
public string LastName { get; set; }
public bool HasGraduated { get; set; }
// This attribute is optional, it sets the format for the Csv.Write method for this DateTime property
[OutputFormat("yyyy-MM-ddTHH:mm:ss.fff")]
public DateTime? GraduationDate { get; set; }
public Student() {
Id = 0;
FirstName = "";
MiddleName = null;
LastName = "";
HasGraduated = false;
GraduationDate = null;
}
}
students.csv:
Id,FirstName,MiddleName,LastName,HasGraduated,GraduationDate
1,Landon,Jameson,Smith,false,null
2,Avery,"Eliza,beth",Davis,true,2000-05-14T17:00:00.000
3,Ethan,,John""son,false,
4,Mia,Grace,Rodriguez,false,null
5,Oliver,William,Brown,true,2002-05-23T16:00:00.000
6,Aria,Rose,Hernandez,false,null
7,Caleb,Alexander,Lee,true,2003-05-16T13:30:00.000
8,Lila,Madison,Turner,false,null
// Reading the CSV
IEnumerable<Student> students = Csv.Read<Student>("students.csv", hasHeader: true);
If you find that the default automatic mapping is just not cutting it, there is a way to define a custom mapping for your object. The Read method is overloaded to take in a Func parameter. This Func is where you'll do your custom mapping.
This Func has one parameter, a string array representing a row in the CSV file. It must return your custom class.
// Reading the CSV with your custom mapping
IEnumerable<Student> students = Csv.Read(
"students.csv",
(string[] row) => {
return new Student() {
Id = Convert.ToInt64(row[0]),
FirstName = row[1],
MiddleName = row[2] == "null" || String.IsNullOrWhiteSpace(row[2]) ? null : row[2],
LastName = row[3],
IsGraduateStudent = Convert.ToBoolean(row[4])
};
},
hasHeader = true
);
Note: Empty column values will return as an empty string ""
IEnumerable<string[]> rows = Csv.Read("students.csv", hasHeader: true);
Things to note:
Student[] data = SomeDataSource();
Csv.Write(data, "students2.csv", true);
// Doesn't have to be of type string[][], it can be any collection of string collections (i.e. any type that conforms to IEnumerable<IEnumerable<string>>)
string[][] data = SomeDataSource();
Csv.Write(data, "students2.csv");
| Version | Supported |
|---|---|
| 2.x | :white_check_mark: |
| 1.x | :x: |