CSV Reports
ClassListToCsvFile<T>(IEnumerable<T>,String,List<String>) Method
Example 



KellermanSoftware.CsvReports Namespace > CsvWriter Class > ClassListToCsvFile Method : ClassListToCsvFile<T>(IEnumerable<T>,String,List<String>) Method
The type of the class
The list of objects
The fully qualified path
A list of the property names
Create a CSV file from a list of objects. It will output the passed in property names as columns.
Syntax
Public Overloads Sub ClassListToCsvFile(Of T As Class)( _
   ByVal list As IEnumerable(Of T), _
   ByVal filePath As String, _
   ByVal propertyNameOrder As List(Of String) _
) 
Dim instance As CsvWriter
Dim list As IEnumerable(Of T)
Dim filePath As String
Dim propertyNameOrder As List(Of String)
 
instance.ClassListToCsvFile(Of T)(list, filePath, propertyNameOrder)
public void ClassListToCsvFile<T>( 
   IEnumerable<T> list,
   string filePath,
   List<string> propertyNameOrder
)
where T: class
public:
void ClassListToCsvFilegeneric<typename T>
( 
   IEnumerable<T^>^ list,
   String^ filePath,
   List<String^>^ propertyNameOrder
) 
where T: ref class

Parameters

list
The list of objects
filePath
The fully qualified path
propertyNameOrder
A list of the property names

Type Parameters

T
The type of the class
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");
 
csvWriter.ClassListToCsvFile(persons, filePath, 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")
 
csvWriter.ClassListToCsvFile(persons, filePath, propertyOrder)
 
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
Overload List