Hi there

 

the NuGet package Tnx.ToCsv has been updated to the newly TNX.CSV

This new edition, adds interesting features like CSV parsing in multiple formats supporting ASP.NET and dynamic objects

 

Here some code example

 

 

var fname = “export.csv”;
File.Delete(fname);

//exports an Enumerable<T> to a stream
//overload available as helper method to export to a HttpResponse object for ASP.NET programmers
using (var f = File.OpenWrite(fname))
new[]{
new { Name=”Mario”, Surname=”Rossi”},
new { Name=”马里奥”, Surname=”罗西”},
}.ToCsvStream(f, encoding: Encoding.Unicode);

//exports a DataTable to a stream
//overload available as helper method to export to a HttpResponse object for ASP.NET programmers
using (var dt = new DataTable())
using (var f = File.OpenWrite(fname))
{
dt.Columns.Add(“Name”, typeof(string));
dt.Columns.Add(“Surname”, typeof(string));
dt.Rows.Add(“Mario”, “Rossi”);
dt.Rows.Add(“马里奥”, “罗西”);
dt.ToCsvStream(f, encoding: Encoding.Unicode);
}

//reads csv as datatable
using (var f = File.OpenRead(fname))
{
var csv1 = f.FromCsvDataTable();
}

//reads csv as IEnumerable<IDictionary<string,object>>
using (var f = File.OpenRead(fname))
{
var csv2 = f.FromCsvDictionary().ToArray();
}

//reads csv as IEnumerable<dynamic>
using (var f = File.OpenRead(fname))
{
var csv3 = f.FromCsvDynamic().ToArray();
dynamic first = csv3.First();
string name = first.Name; //dynamic here
}

One thought on “TNX.CSV [new version 1.1.x]”

Lascia un commento