Skip to content

Simple c# (.NET 5) table library that renders any List<T> into a nicely formatted markdown, csv, html or console table, allowing for extra formats.

License

Notifications You must be signed in to change notification settings

lanicon/table.lib

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

table.lib

Simple c# (.NET 5) table library that renders any List<T> into a nicely formatted markdown, csv, html or console table, allowing for extra formats.

Markdown format

Table<TestClass>.Add(list).ToConsole();
| Field1 | Field2               | Field3        | Field4 | Field5      | Field6 |
| ------ | -------------------- | ------------- | ------ | ----------- | ------ |
| 321121 | Hi 312321            | 2,121.32      | True   | 01-Jan-1970 | 34.43  |
| 32321  | Hi long text         | 21,111,111.32 | True   | 01-Jan-1970 | 34.43  |
| 321    | Hi longer text       | 2,121.32      | True   | 01-Jan-1970 | 34.43  |
| 13     | Hi very long text    | 21,111,121.32 | True   | 01-Jan-1970 | 34.43  |
| 13     | Hi very, long text   | 21,111,121.32 | True   | 01-Jan-1970 | 34.43  |
| 13     | Hi "very" long  text | 21,111,121.32 | True   | 01-Jan-1970 | 34.43  |
Field1 Field2 Field3 Field4 Field5 Field6
321121 Hi 312321 2,121.32 True 01-Jan-1970 34.43
32321 Hi long text 21,111,111.32 True 01-Jan-1970 34.43
321 Hi longer text 2,121.32 True 01-Jan-1970 34.43
13 Hi very long text 21,111,121.32 True 01-Jan-1970 34.43
13 Hi very, long text 21,111,121.32 True 01-Jan-1970 34.43
13 Hi "very" long text 21,111,121.32 True 01-Jan-1970 34.43

Considerations:

  • Any CRLF will be automatically transformed into a space for an easy representation of the output

Dynamic Fields

If the List contains another collection of , the library is able to scan those and build the resultant dataset giving them a column name called DynamicX:

var test = new List<IEnumerable<string>>
{
    new List<string>() {"AAA", "BBB", "CCC"},
    new List<string>() {"AAA", "BBB", "CCC"},
    new List<string>() {"AAA", "BBB", "CCC"},
    new List<string>() {"AAA", "BBB", "CCC"}
};

Table<IEnumerable<string>>.Add(test).ToConsole();
| Capacity | Count | Dynamic0 | Dynamic1 | Dynamic2 |
| -------- | ----- | -------- | -------- | -------- |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
Capacity Count Dynamic0 Dynamic1 Dynamic2
4 3 AAA BBB CCC
4 3 AAA BBB CCC
4 3 AAA BBB CCC
4 3 AAA BBB CCC

Column Name change

If the name of the column is not of your liking, you can change it via OverrideColumnsNames and provide your preferred name. Note that this will also alter the column width to allow for more room if the new name is larger than the previous one.

Table<IEnumerable<string>>.Add(test).
    OverrideColumnsNames(new Dictionary<string, string> {{"Dynamic0","ColumnA"}}).
    ToConsole();
| Capacity | Count | ColumnA  | Dynamic1 | Dynamic2 |
| -------- | ----- | -------- | -------- | -------- |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
| 4        | 3     | AAA      | BBB      | CCC      |
Capacity Count ColumnA Dynamic1 Dynamic2
4 3 AAA BBB CCC
4 3 AAA BBB CCC
4 3 AAA BBB CCC
4 3 AAA BBB CCC

Column filtering

You don't want to show all the columns? Easy, just use the FilterOutColumns property:

Table<IEnumerable<string>>.Add(test).
    OverrideColumnsNames(new Dictionary<string, string> { { "Dynamic0", "ColumnA" } }).
    FilterOutColumns(new []{ "Capacity", "Count"}).
    ToConsole();
| ColumnA  | Dynamic1 | Dynamic2 |
| -------- | -------- | -------- |
| AAA      | BBB      | CCC      |
| AAA      | BBB      | CCC      |
| AAA      | BBB      | CCC      |
| AAA      | BBB      | CCC      |
ColumnA Dynamic1 Dynamic2
AAA BBB CCC
AAA BBB CCC
AAA BBB CCC
AAA BBB CCC

Column Justification

You need one of the columns, right aligned or centered? Use the column justification option.

Table<IEnumerable<string>>.Add(test).
    OverrideColumnsNames(new Dictionary<string, string>
    {
        { "Dynamic0", "A" },
        { "Dynamic1", "B" },
        { "Dynamic2", "C" }
    }).
    FilterOutColumns(new[] { "Capacity", "Count" }).
    ColumnContentTextJustification(new Dictionary<string, TextJustification>
    {
        {"Dynamic0", TextJustification.Right}, 
        { "Dynamic1", TextJustification.Centered }
    }).
    ToConsole();

Note that this will only affect markdown, console and html outputs and only their data. Columns labels will remain left aligned.

|    A     |    B     |    C     |
| -------- | -------- | -------- |
|      AAA |   BBB    | CCC      |
|      AAA |   BBB    | CCC      |
|      AAA |   BBB    | CCC      |
|      AAA |   BBB    | CCC      |
ColumnA Dynamic1 Dynamic2
AAA BBB CCC
AAA BBB CCC
AAA BBB CCC
AAA BBB CCC

HTML Output

Transform your output into a nicely formatted HTML table

Table<IEnumerable<string>>.Add(test).
    OverrideColumnsNames(new Dictionary<string, string> { { "Dynamic0", "ColumnA" } }).
    FilterOutColumns(new []{ "Capacity", "Count"}).
    ToHtml(@"C:\temp\test.html");

Table<TestClass>.Add(list).
    ToHtml(@"C:\temp\test-list.html");

Sample generated code:

<table style="border-collapse: collapse; width: 100%;">
<tr>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field1</th>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field2</th>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field3</th>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field4</th>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field5</th>
<th style="text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Field6</th>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">321121</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi 312321</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">2,121.32</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">32321</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi long text</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">21,111,111.32</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">321</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi longer text</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">2,121.32</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">13</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi very long text</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">21,111,121.32</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">13</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi very, long text</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">21,111,121.32</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
<tr>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">13</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">Hi "very" long<br> text</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">21,111,121.32</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">True</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">01-Jan-1970</td>
<td style="text-align: right; color: black; background-color: #f2f2f2;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;">34.43</td>
</tr>
</table>

Sample output:

Field1 Field2 Field3 Field4 Field5 Field6
321121 Hi 312321 2,121.32 True 01-Jan-1970 34.43
32321 Hi long text 21,111,111.32 True 01-Jan-1970 34.43
321 Hi longer text 2,121.32 True 01-Jan-1970 34.43
13 Hi very long text 21,111,121.32 True 01-Jan-1970 34.43
13 Hi very, long text 21,111,121.32 True 01-Jan-1970 34.43
13 Hi "very" long
text
21,111,121.32 True 01-Jan-1970 34.43

Real HTML output:

htmlresult

CSV Output

Trasform your output into a nicely formatted CSV file

Table<TestClass>.Add(list).
    ToCsv(@"C:\temp\test-list.csv");

The format of the file can be seen here:

Field1,Field2,Field3,Field4,Field5,Field6
321121,Hi 312321,"2,121.32",True,01-Jan-1970,34.43
32321,Hi long text,"21,111,111.32",True,01-Jan-1970,34.43
321,Hi longer text,"2,121.32",True,01-Jan-1970,34.43
13,Hi very long text,"21,111,121.32",True,01-Jan-1970,34.43
13,"Hi very, long text","21,111,121.32",True,01-Jan-1970,34.43
13,"Hi ""very"" long
 text","21,111,121.32",True,01-Jan-1970,34.43

Note that we use the CSV standard when processing CRLF, " or , characters surrouding the value with double quotes.

csvoutput

About

Simple c# (.NET 5) table library that renders any List<T> into a nicely formatted markdown, csv, html or console table, allowing for extra formats.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%