Sunday, March 24, 2013

Tabular v1.0 - Import/Export Tabular Data to Excel and CSV

Awhile ago, I wrote a small library for describing, importing, and exporting tabular data to and from Excel XML, and CSV formats. I just never got around to releasing it, but I'm making a more concerted effort recently to push releases forward. The full documentation is online, and the binaries here. The license is LGPL.

Describing Tabular Data

Tabular.dll is an assembly via which you can declaratively describe tabular data:

var table1 = new Table
{
    Name = "SomeTable",
    Rows =
    {
        new Row { 1, "foo", 5.7 },
        new Row { 2, "bar", 99.99M },
        new Row { 3, "baz", 0.0 },
    }
};

There are really only three classes of interest, Table, Row, and Cell. A table consists of a series of rows, a row consists of a series of cells, and each cell consists of a string value together with an alleged data type describing the string contents.

The DataType enumeration is the list of recognized data strings. Cell provides numerous implicit coercions from CLR types to Cell and sets the appropriate data type, so declarative tables are simple to describe as the above code sample demonstrates.

It's also quite simple to describe a table that's derive from some enumerable source:

IEnumerable list = ...
var table = new Table
{
    Name = "EnumerableTable",
    Bind = list.Select((x,i) => new Row { i, x })
}

You can also imperatively build up the table using the AddRow method.

Import/Export of CSV Data

Tabular.Csv.dll provides import and export of tabular data to CSV format:

var table1 = new Table
{
    Name = "SomeTable",
    Rows =
    {
        new Row { 1, "foo", 5.7 },
        new Row { 2, "bar", 99.99M },
        new Row { 3, "baz", 0.0 },
    }
};
var csv = Csv.ToCsv(table1, CsvFormat.Quoted);
Console.WriteLine(csv.Data);

// output:
// "1","foo","5.7"
// "2","bar","99.99"
// "3","baz","0"

var table2 = Csv.FromCsv(csv);
// table2 is equivalent to table1

The CsvFormat enumeration describes whether the CSV data should be formatted in the safer quoted data format, or in the less safe raw format.

Import/Export of Excel Data

Tabular.Excel.dll provide import and export features for Excel XML file format. Excel 2002 XML schema is used for simplicity. See the docs for the full details, but here's a relatively straightforward overview:

var table1 = new Table
{
    Name = "SomeTable",
    Rows =
    {
        new Row { 1, "foo", 5.7 },
        new Row { 2, "bar", 99.99M },
        new Row { 3, "baz", 0.0 },
    }
};
// generate in-memory XDocument that you can work with
XDocument excel = Excel.ToXml(table1);

// or write directly to stream
using (var stream = File.OpenWrite("foo.xml"))
{
    Excel.WriteTo(table1, stream);
}

// you can also import the xml data
Table table2 = Excel.FromXml(excel);

// or import by reading from a stream
using (var stream = File.OpenRead("foo.xml"))
{
    table2 = Excel.ReadFrom(stream);
}

Edit: just an update, I've uploaded the Tabular v1.0 release to NuGet.

No comments: