CSV Reports
DataTableToCsvWriter(TextWriter,DataTable,List<String>) Method
Example 



KellermanSoftware.CsvReports Namespace > CsvWriter Class > DataTableToCsvWriter Method : DataTableToCsvWriter(TextWriter,DataTable,List<String>) Method
The output text writer
The table to output
The order of the columns to output
Output the rows of a data table to a text writer
Syntax
Public Overloads Sub DataTableToCsvWriter( _
   ByVal writer As TextWriter, _
   ByVal table As DataTable, _
   ByVal columnNameOrder As List(Of String) _
) 
Dim instance As CsvWriter
Dim writer As TextWriter
Dim table As DataTable
Dim columnNameOrder As List(Of String)
 
instance.DataTableToCsvWriter(writer, table, columnNameOrder)
public void DataTableToCsvWriter( 
   TextWriter writer,
   DataTable table,
   List<string> columnNameOrder
)
public:
void DataTableToCsvWriter( 
   TextWriter^ writer,
   DataTable^ table,
   List<String^>^ columnNameOrder
) 

Parameters

writer
The output text writer
table
The table to output
columnNameOrder
The order of the columns to output
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
 
//Parameters
DataTable table = new DataTable();
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("LastName", typeof(string));
table.Columns.Add("BirthDate", typeof(string));
 
DataRow row = table.NewRow();
row["FirstName"] = "John";
row["LastName"] = "Smith";
row["BirthDate"] = DateTime.Now.AddDays(-1).ToString();
table.Rows.Add(row);
 
//Order the output by Last Name, First Name, Birth Date
List<string> columnNameOrder = new List<string>();
columnNameOrder.Add("LastName");
columnNameOrder.Add("FirstName");
columnNameOrder.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.DataTableToCsvWriter(writer, table, columnNameOrder);
    }
}
 
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
 
'Parameters
Dim table As New DataTable()
table.Columns.Add("FirstName", GetType(String))
table.Columns.Add("LastName", GetType(String))
table.Columns.Add("BirthDate", GetType(String))
 
Dim row As DataRow = table.NewRow()
row("FirstName") = "John"
row("LastName") = "Smith"
row("BirthDate") = Date.Now.AddDays(-1).ToString()
table.Rows.Add(row)
 
'Order the output by Last Name, First Name, Birth Date
Dim columnNameOrder As New List(Of String)()
columnNameOrder.Add("LastName")
columnNameOrder.Add("FirstName")
columnNameOrder.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.DataTableToCsvWriter(writer, table, columnNameOrder)
    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
Overload List