是否有任何.NET库在控制台(textmode)中绘制表格?

时间:2009-09-16 09:44:18

标签: .net console

在为自己编写一个小型C#应用程序时,我意识到如果我可以轻松地在textmode中绘制表格,它会很简洁。你知道,像这样:

+-----------------+-----------------+
|     Header 1    |    Header 2     |
+--------+--------+--------+--------+
| Data 1 | Data 2 | Data 3 | Data 4 |
| Data 1 | Data 2 | Data 3 | Data 4 |
| Data 1 | Data 2 | Data 3 | Data 4 |
+--------+--------+--------+--------+

快速谷歌搜索没有透露任何信息。有没有这样的现成品,或者我应该推出自己的产品?

已添加:理想版本支持:

  • 行/列跨度;
  • 不同的边框宽度和样式;
  • 水平和垂直文字对齐

但我也会满足于此。 :)

1 个答案:

答案 0 :(得分:9)

这是你要找的那个 http://www.phpguru.org/static/ConsoleTable.html 要么 http://www.phpguru.org/downloads/csharp/ConsoleTable/ConsoleTable.cs

ConsoleTable table = new ConsoleTable();

table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});

table.SetHeaders(new string[] {"First Column", "Second Column", "Third Column"});
table.SetFooters(new string[] {"Yabba"});

table.InsertRow(new string[] {"", "ferfr", "frf        r"}, 7);
table.PrependRow(new string[] {});

System.Console.Write(table.ToString());

Produces...

+--------------+---------------+--------------+
| First Column | Second Column | Third Column |
+--------------+---------------+--------------+
|              |               |              |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
|              |               |              |
|              |               |              |
|              |               |              |
|              | ferfr         | frf        r |
+--------------+---------------+--------------+
| Yabba        |               |              |
+--------------+---------------+--------------+