Thursday, December 15, 2016

Helpers: TableViewer Text Export

Today I would like to present a small helper class that exports the content of a TableViewer to a String representation.

How to use

The default implementation creates a CSV output. Exporting the table is simple:
TableExporter exporter = new TableExporter(tableViewer);
exporter.print(outputStream);

// exporter.toString() // serializes into a String
The export uses sorting, filtering and column ordering so your output matches to what you see on the screen.

Other output formats

If you are interested in other output formats, have a look at HTMLTableExporter, which renders the same data into an HTML table. You can easily extend the base class to adapt the output format to your own needs.

Examples



rendered as CSV text:

First,Last,City
Obi-Wan,Kenobi,Tatooine
Leia,Organa,Alderaan
Luke,Skywalker,Tatooine
Han,Solo,Corellia

...or as HTML table

First Last City
Obi-Wan Kenobi Tatooine
Leia Organa Alderaan
Luke Skywalker Tatooine
Han Solo Corellia