9d59976f ae0e 32f4 31f4 b014969572b7 - akesseler/Plexdata.CsvParser GitHub Wiki
This static class allows writing a value list of TInstance types either into a file or simply into a stream (which actually can also be a file).
System.Object
Plexdata.CsvParser.Processors.CsvExporter(TInstance)
Namespace: Plexdata.CsvParser.Processors
Assembly: Plexdata.CsvParser.NET (in Plexdata.CsvParser.NET.dll) Version: 1.1.3+d5bef99aa35461ce4bafadf0bf2c2aeca18044ea
C#
public static class CsvExporter<TInstance>
where TInstance : class
- TInstance
- The class type that fully describes the structure of the CSV output to be written.
Name | Description | |
---|---|---|
![]() ![]() |
BuildLine | This method tries to build a list of objects the represent a single CSV line. |
![]() ![]() |
Save(IEnumerable(TInstance), Stream) | This method tries to save given values into given stream. |
![]() ![]() |
Save(IEnumerable(TInstance), String) | This method tries to save given values into given file. |
![]() ![]() |
Save(IEnumerable(TInstance), Stream, CsvSettings) | This method tries to save given values into given stream. |
![]() ![]() |
Save(IEnumerable(TInstance), String, CsvSettings) | This method tries to save given values into given file. |
![]() ![]() |
Save(IEnumerable(TInstance), String, CsvSettings, Boolean) | This method tries to save given values into given file. |
![]() ![]() |
WriteHead | This method tries to write the header information into given stream according to given parameter set. |
![]() ![]() |
WriteLine | This method tries to write a particular line into given stream according to given parameter set. |
CSV actually means Comma Separated Values. Sometimes it is also called as Character Separated Values. But no matter which name is used, CSV always represents a text file mainly used for data exchange between different system.
It would be possible (using a proper configuration) to write a CSV output according to the rules of RFC 4180. For more information about RFC 4180 please visit the web-site under https://www.ietf.org/rfc/rfc4180.txt
This section wants to show a simple but hopefully useful example of how to use the CSV Parser for data exports.
using Plexdata.CsvParser.Attributes;
using Plexdata.CsvParser.Processors;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace MyCsvExporter
{
class Program
{
[CsvDocument]
class CsvCustomer
{
[CsvIgnore]
public Int32 Id { get; set; }
[CsvColumn(Offset = 2, Header = "Identifier")]
public Int32 ExternalId { get; set; }
[CsvColumn(Offset = 1, Header = "Forename")]
public String FirstName { get; set; }
[CsvColumn(Offset = 0, Header = "Surname")]
public String LastName { get; set; }
[CsvColumn(Offset = 5, Header = "Active")]
public Boolean IsActive { get; set; }
[CsvColumn(Offset = 3, Header = "Date")]
public DateTime? EntryDate { get; set; }
[CsvColumn(Offset = 4, Header = "Sales")]
public Decimal SalesAverage { get; set; }
[CsvColumn(Offset = 6, Header = "Notes")]
public String Description { get; set; }
}
static void Main(String[] args)
{
try
{
String filename = @"C:\folder\file.csv";
List<CsvCustomer> customers = new List<CsvCustomer>
{
new CsvCustomer {
LastName = "Marley",
FirstName = "Bob",
ExternalId = 1001,
EntryDate = new DateTime(2007, 5, 3),
SalesAverage = 1234.56m,
IsActive = false,
Description = "Have a short note here." },
new CsvCustomer {
LastName = "Monroe",
FirstName = "Marilyn",
ExternalId = 1002,
EntryDate = new DateTime(2008, 6, 5),
SalesAverage = 1234.56m,
IsActive = false,
Description = null },
new CsvCustomer {
LastName = "Snipes",
FirstName = "Wesley",
ExternalId = 1003,
EntryDate = new DateTime(2009, 7, 6),
SalesAverage = 1234.56m,
IsActive = true,
Description = "Have a short note here." },
new CsvCustomer {
LastName = "Hurley",
FirstName = "Elizabeth",
ExternalId = 1004,
EntryDate = new DateTime(2005, 8, 8),
SalesAverage = 1234.56m,
IsActive = true,
Description = "Have a short note here." },
};
CsvSettings settings = new CsvSettings
{
Culture = CultureInfo.GetCultureInfo("en-US"),
Textual = true,
Mappings = new CsvMappings
{
TrueValue = "yeah",
FalseValue = "nope",
},
};
// Output file would contain this content:
// Surname,Forename,Identifier,Date,Sales,Active,Notes
// "Marley","Bob",1001,2007-05-03T00:00:00,1234.56,nope,"Have a short note here."
// "Monroe","Marilyn",1002,2008-06-05T00:00:00,1234.56,nope,
// "Snipes","Wesley",1003,2009-07-06T00:00:00,1234.56,yeah,"Have a short note here."
// "Hurley","Elizabeth",1004,2005-08-08T00:00:00,1234.56,yeah,"Have a short note here."
CsvExporter<CsvCustomer>.Save(customers, filename, settings);
Console.ReadKey();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}