Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions files-csharp/WriteToCsv/WriteToCsv/Contact.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace WritingCSV
using CsvHelper;

namespace WritingCSV;

[CsvRecord]
public partial class Contact
{
public class Contact
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
80 changes: 37 additions & 43 deletions files-csharp/WriteToCsv/WriteToCsv/Employee.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
using CsvHelper.Configuration.Attributes;
using CsvHelper;

namespace WritingCSV
namespace WritingCSV;

// The class-level [CsvOptions] takes effect only through options built from
// the class, CsvWriterOptions.For<Employee>(); a writer created with the
// default options ignores it.
[CsvRecord]
[CsvOptions(Separator = ';', HasHeader = false)]
public partial class Employee
{
// The two class-level attributes take effect only through a
// configuration built with the class's attributes, for example
// CsvConfiguration.FromAttributes<Employee>(CultureInfo.InvariantCulture);
// a CsvWriter built from a CultureInfo alone ignores them.
[Delimiter(";")]
[HasHeaderRecord(false)]
public class Employee
{
[Name("Identifier")]
[Index(0)]
public int Id { get; set; }

[Name("Full Name")]
[Index(1)]
public string Name { get; set; } = string.Empty;

[Index(2)]
[Format("yyyy-MM-dd")]
public DateTime Hired { get; set; }

[Index(3)]
[CultureInfo("en-US")]
[Format("C")]
public decimal Salary { get; set; }

[Index(4)]
[BooleanTrueValues("yes")]
[BooleanFalseValues("no")]
public bool Active { get; set; }

[Index(5)]
[Constant("EU")]
public string Region { get; set; } = string.Empty;

[Index(6)]
[NullValues("N/A")]
public string? Nickname { get; set; }

[Ignore]
public string Notes { get; set; } = string.Empty;
}
[CsvColumn("Identifier", Index = 0)]
public int Id { get; set; }

[CsvColumn("Full Name", Index = 1)]
public string Name { get; set; } = string.Empty;

[CsvColumn(Index = 2)]
[CsvFormat("yyyy-MM-dd")]
public DateTime Hired { get; set; }

[CsvColumn(Index = 3)]
[CsvCulture("de-DE")]
public decimal Salary { get; set; }

[CsvColumn(Index = 4)]
[CsvBooleanValues("yes", "no")]
public bool Active { get; set; }

[CsvColumn(Index = 5)]
[CsvConstant("EU")]
public string Region { get; set; } = string.Empty;

[CsvColumn(Index = 6)]
[CsvNullValues("N/A")]
public string? Nickname { get; set; }

[CsvIgnore]
public string Notes { get; set; } = string.Empty;
}
32 changes: 13 additions & 19 deletions files-csharp/WriteToCsv/WriteToCsv/Person.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using CsvHelper.Configuration.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;

namespace WritingCSV
namespace WritingCSV;

[CsvRecord]
public partial class Person
{
public class Person
{
[Name("Identifier")]
[Index(0)]
public int Id { get; set; }
[Index(2)]
public string Name { get; set; } = string.Empty;
[Index(1)]
public bool IsLiving { get; set; }
[Format("yyyy-MM-dd")]
public DateTime DateOfBirth { get; set; }
}
[CsvColumn("Identifier", Index = 0)]
public int Id { get; set; }
[CsvColumn(Index = 2)]
public string Name { get; set; } = string.Empty;
[CsvColumn(Index = 1)]
public bool IsLiving { get; set; }
[CsvFormat("yyyy-MM-dd")]
public DateTime DateOfBirth { get; set; }
}
30 changes: 19 additions & 11 deletions files-csharp/WriteToCsv/WriteToCsv/PersonMap.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
using CsvHelper.Configuration;
using CsvHelper;

namespace WritingCSV
namespace WritingCSV.Mapped;

// A class we cannot annotate, such as one from another library.
public class Person
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsLiving { get; set; }
}

[CsvRecordMap(typeof(Person))]
public partial class PersonMap
{
public sealed class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Map(p => p.Id).Name("Identifier").Index(0);
Map(p => p.IsLiving).Index(1);
Map(p => p.Name).Index(2);
}
}
[CsvColumn("Identifier", Index = 0)]
public int Id { get; set; }
[CsvColumn(Index = 2)]
public string Name { get; set; } = string.Empty;
[CsvColumn(Index = 1)]
public bool IsLiving { get; set; }
}
198 changes: 89 additions & 109 deletions files-csharp/WriteToCsv/WriteToCsv/WriteMethods.cs
Original file line number Diff line number Diff line change
@@ -1,138 +1,118 @@
using CsvHelper;
using CsvHelper.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WritingCSV

namespace WritingCSV;

public static class WriteMethods
{
public static class WriteMethods
public static void WritePersonsWithDoB()
{
using var csv = CsvWriter.ToFile("filePersonsWithDoB.csv");

public static void WritePersonsWithDoB()
{
var myPersons = new List<Person>()
{
new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = Convert.ToDateTime("03/05/2006") },
new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = Convert.ToDateTime("03/09/1998") },
new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = Convert.ToDateTime("03/08/1994") }
};
csv.WriteRecords(CreatePersons());
}

using (var writer = new StreamWriter("filePersonsWithDoB.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(myPersons);
}
public static List<Person> CreatePersons() =>
[
new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = new DateTime(2006, 3, 5) },
new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = new DateTime(1998, 3, 9) },
new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = new DateTime(1994, 3, 8) }
];

}
public static void WriteWithDefaultSettings(string path = "filePersons.csv")
{
using var csv = CsvWriter.ToFile(path);

csv.WriteRecords(CreatePersons());
}

public static List<Person> CreatePersons() => new()
public static void WriteWithoutHeader(string path = "filePersons.csv")
{
var options = new CsvWriterOptions
{
new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = Convert.ToDateTime("03/05/2006") },
new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = Convert.ToDateTime("03/09/1998") },
new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = Convert.ToDateTime("03/08/1994") }
HasHeader = false
};

public static void WriteWithDefaultSettings(string path = "filePersons.csv")
{
using (var writer = new StreamWriter(path))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(CreatePersons());
}
}
using var csv = CsvWriter.ToFile(path, options);

public static void WriteWithoutHeader(string path = "filePersons.csv")
{
var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
csv.WriteRecords(CreatePersons());
}

using (var writer = new StreamWriter(path))
using (var csv = new CsvWriter(writer, configPersons))
{
csv.WriteRecords(CreatePersons());
}
}
public static void WriteWithRecordMap(string path = "filePersonsMapped.csv")
{
List<Mapped.Person> persons =
[
new() { Id = 1, IsLiving = true, Name = "John" },
new() { Id = 2, IsLiving = true, Name = "Steve" },
new() { Id = 3, IsLiving = true, Name = "James" }
];

public static void WriteWithClassMap(string path = "filePersons.csv")
{
using var writer = new StreamWriter(path);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
using var csv = CsvWriter.ToFile(path);

csv.Context.RegisterClassMap<PersonMap>();
csv.WriteRecords(CreatePersons());
}
csv.WriteRecords(persons);
}

public static void AppendPersons(string path = "filePersons.csv")
public static void AppendPersons(string path = "filePersons.csv")
{
var options = new CsvWriterOptions
{
var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
HasHeader = false
};

using (var stream = File.Open(path, FileMode.Append))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvWriter(writer, configPersons))
{
csv.WriteRecords(CreatePersons());
}
}
using var stream = File.Open(path, FileMode.Append);
using var csv = CsvWriter.Create(stream, options);

public static string WriteTrickyValues()
{
var trickyContacts = new List<Contact>()
{
new Contact { Id = 1, Name = "Doe, John" },
new Contact { Id = 2, Name = "He said \"hi\"" },
new Contact { Id = 3, Name = "line1\nline2" }
};

using var writer = new StringWriter();
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(trickyContacts);
}
csv.WriteRecords(CreatePersons());
}

return writer.ToString();
public static string WriteTrickyValues(CsvWriterOptions? options = null)
{
List<Contact> trickyContacts =
[
new Contact { Id = 1, Name = "Doe, John" },
new Contact { Id = 2, Name = "He said \"hi\"" },
new Contact { Id = 3, Name = "line1\nline2" },
new Contact { Id = 4, Name = "=1+1" }
];

using var writer = new StringWriter();
using (var csv = CsvWriter.Create(writer, options, leaveOpen: true))
{
csv.WriteRecords(trickyContacts);
}

public static async Task WritePersonsAsync(string path = "filePersons.csv")
{
await using var writer = new StreamWriter(path);
await using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
return writer.ToString();
}

await csv.WriteRecordsAsync(CreatePersons());
}
public static async Task WritePersonsAsync(string path = "filePersons.csv")
{
await using var csv = CsvWriter.ToFile(path);

public static string WriteEmployees(IWriterConfiguration configuration)
{
var employees = new List<Employee>()
{
new Employee
{
Id = 1,
Name = "Doe, John",
Hired = new DateTime(2020, 3, 5),
Salary = 1234.50m,
Active = true,
Region = "US",
Nickname = null,
Notes = "not written"
}
};

using var writer = new StringWriter();
using (var csv = new CsvWriter(writer, configuration))
await csv.WriteRecordsAsync(CreatePersons());
}

public static string WriteEmployees(CsvWriterOptions? options = null)
{
List<Employee> employees =
[
new Employee
{
csv.WriteRecords(employees);
Id = 1,
Name = "Doe, John",
Hired = new DateTime(2020, 3, 5),
Salary = 1234.50m,
Active = true,
Region = "US",
Nickname = null,
Notes = "not written"
}
];

return writer.ToString();
using var writer = new StringWriter();
using (var csv = CsvWriter.Create(writer, options, leaveOpen: true))
{
csv.WriteRecords(employees);
}

return writer.ToString();
}
}
Loading
Loading