The csv format file is sometimes necessary because it can be displayed / edited with Excel and is simple.
There are other libraries that read and write csv already, but I tried to make it simpler.
With this library, you can read and write plain object collections (IEnumerable<Something>) in csv format.
How to use
Overview of how to use
First, prepare something IEnumerable<TElement>:
An IEnumerable<TElement> something
IEnumerable<ToDo> toDoes = new ToDoList();
For example this class:
class ToDoList : IEnumerable<ToDo> // sample collection
{
public IEnumerator<ToDo> GetEnumerator()
{
yield return new ToDo { Id = 1, Title = "filing tax returns", Deadline = new DateTime(2018, 12, 1) };
yield return new ToDo { Id = 2, Title = "report of a business trip", Detail = "\"ASAP\"", DaySpan = new DaySpan(3), Priority = Priority.High };
yield return new ToDo { Id = 3, Title = "expense slips", Detail = "book expenses: \"C# 6.0 and the .NET 4.6 Framework\",\"The C# Programming\"", Priority = Priority.Low, Done = true };
yield return new ToDo { Id = 4, Title = " wish list ", Detail = " \t (1) \"milk\"\n \t (2) shampoo\n \t (3) tissue ", Priority = Priority.High };
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Public properties with both "get" and "set" of each element in the collection are read and written to csv files.
When writing:
When writing, it is converted to a string with the "ToString()" method regardless of the type.
When reading:
When reading, the string type is left as is, and enum (enumeration type) is read as the value of the string.
For other types, it tries to change the string to a value using "TryParse" or "Parse".
Types that cannot do either of these will not be read.
Properties that have both "get" and "set" and are of one of the following types are read:
String type
enum
A type that has a default constructor and can be "TryParse" or "Parse"
(Basic numeric types such as int, DateTime, user-defined types with "TryParse" or "Parse")
Other rules
Properties with the [CsvIgnore ()] attribute are not read or written.
Properties with the [ColumnName("Column name")] attribute will be changed to the one with the specified column name in the csv file.
For example, the above ToDo class is like this:
// Sample element type
// Properties marked with ✔ will be read / written
// Properties marked with ✖ won't be read / written
class ToDo
{
public int Id { get; set; } // ✔
public string Title { get; set; } = ""; // ✔
public DateTime Deadline { get; set; } = DateTime.Now; // ✔
public bool Done { get; set; } // ✔
public Priority Priority { get; set; } = Priority.Middle; // ✔ user-defined enum
[ColumnName("Details")]
public string Detail { get; set; } = ""; // ✔ [ColumnName ("Details")] changes the column name in the csv file to "Details"
public DaySpan DaySpan { get; set; } // ✔ User-defined type with "Parse" (without "TryParse")
[CsvIgnore()]
public string Option { get; set; } = ""; // ✖ Ignored because [CsvIgnore()] is attached
public string Version => "1.0"; // ✖ Ignored because it is only a get property
}
The user-defined types used in the above ToDo class are as follows:
// User-defined enum example
enum Priority { High, Middle, Low }
// Example of a user-defined type with "Parse" (without "TryParse")
struct DaySpan
{
public int Value { get; private set; }
public DaySpan(int value) => Value = value;
public static DaySpan Parse(string text) => new DaySpan(int.Parse(text));
public override string ToString() => Value.ToString();
}
With or without header
csv can be read and written even if it has no header part (eg the first line of the csv file above).
However, if there is a header part, it can be read by collating the header part even if the column is switched, but if there is no header part, it cannot be read if the column is switched.
Microsoft MVP for Development Tools - Visual C# (Jul. 2005 - Dec. 2014) Microsoft MVP for .NET (Jan. 2015 - Oct. 2015) Microsoft MVP for Visual Studio and Development Technologies (Nov. 2015 - Jun. 2018) Microsoft MVP for Developer Technologies (Jul. 2018 - Jun. 2021) My MVP Profile