CSV Reports
ClassListToCsvWriter<T> Method
Example 



KellermanSoftware.CsvReports Namespace > CsvWriter Class : ClassListToCsvWriter<T> Method
Class type
The output text writer
List of class objects
The order of the properties to output
Write the properties of a class list into a TextWriter
Syntax
Public Sub ClassListToCsvWriter(Of T As Class)( _
   ByVal writer As TextWriter, _
   ByVal list As IEnumerable(Of T), _
   ByVal propertyNameOrder As List(Of String) _
) 
Dim instance As CsvWriter
Dim writer As TextWriter
Dim list As IEnumerable(Of T)
Dim propertyNameOrder As List(Of String)
 
instance.ClassListToCsvWriter(Of T)(writer, list, propertyNameOrder)
public void ClassListToCsvWriter<T>( 
   TextWriter writer,
   IEnumerable<T> list,
   List<string> propertyNameOrder
)
where T: class
public:
void ClassListToCsvWritergeneric<typename T>
( 
   TextWriter^ writer,
   IEnumerable<T^>^ list,
   List<String^>^ propertyNameOrder
) 
where T: ref class

Parameters

writer
The output text writer
list
List of class objects
propertyNameOrder
The order of the properties to output

Type Parameters

T
Class type
Example
//Example Class
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}
 
CsvWriter csvWriter = new CsvWriter(); //Trial Mode
//CsvWriter csvWriter = new CsvWriter("place user name here", "place license key here"); //License Mode
 
List<Person> persons = new List<Person>();
Person person1 = new Person();
person1.FirstName = "John";
person1.LastName = "Smith";
person1.BirthDate = DateTime.Now.AddDays(-1);
persons.Add(person1);
 
//Order the output by Last Name, First Name, Birth Date
List<string> propertyOrder = new List<string>();
propertyOrder.Add("LastName");
propertyOrder.Add("FirstName");
propertyOrder.Add("BirthDate");
 
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testWriter.csv");
 
using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
    using (TextWriter writer = new StreamWriter(stream))
    {
        csvWriter.ClassListToCsvWriter(writer, persons, propertyOrder);
    }
}
 
Console.WriteLine(File.ReadAllText(filePath));
'Example Class
Public Class Person
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property BirthDate() As Date
End Class
 
Dim csvWriter As New CsvWriter() 'Trial Mode
'Dim csvWriter As New CsvWriter("place user name here", "place license key here") 'License Mode
 
Dim persons As New List(Of Person)()
Dim person1 As New Person()
person1.FirstName = "John"
person1.LastName = "Smith"
person1.BirthDate = Date.Now.AddDays(-1)
persons.Add(person1)
 
'Order the output by Last Name, First Name, Birth Date
Dim propertyOrder As New List(Of String)()
propertyOrder.Add("LastName")
propertyOrder.Add("FirstName")
propertyOrder.Add("BirthDate")
 
Dim filePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testWriter.csv")
 
Using stream As New FileStream(filePath, FileMode.Create, FileAccess.Write)
    Using writer As TextWriter = New StreamWriter(stream)
        csvWriter.ClassListToCsvWriter(writer, persons, propertyOrder)
    End Using
End Using
 
Console.WriteLine(File.ReadAllText(filePath))
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

CsvWriter Class
CsvWriter Members