2009-09-16 31 views
2

在爲我自己編寫一個小型C#應用程序時,我意識到如果我可以輕鬆在textmode中繪製表格,它會很整潔。你知道,像這樣:是否有任何.NET庫在控制檯(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 | 
+--------+--------+--------+--------+ 

快速谷歌搜索沒有透露任何東西。有沒有這樣的現成的,或者我應該推出自己的?

補充:理想的版本將支持:

  • 行/列跨越;
  • 不同的邊框寬度和樣式;
  • 水平和垂直文本對齊

但我會滿足於少爲好。 :)

+0

如果我的回答讓您滿意的一個,請接受它;-) – 2009-09-16 10:27:52

+1

我會的,但我會先等一會看看是否有更接近我上面提到的理想版本。 :) – 2009-09-16 10:42:17

回答

9

這是你正在尋找 http://www.phpguru.org/static/ConsoleTable.htmlhttp://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  |    |    | 
+--------------+---------------+--------------+ 
+0

顯然這是最好的。好吧。猜猜我會推出我自己的。 :) – 2009-09-16 10:50:49

相關問題