2014-04-29 89 views
0

我有一個存儲在我想打印到控制檯窗口,它是所有排隊正確的2維列表數據。VB.NET打印格式化的表格在控制檯窗口

例子:

Dim aList As New List Of(List Of String)) 
aList = AfunctionThatFetchesData 

ALIST

{column 1}   {column 2} {column 3} 
This is some data 0   3 
Some more   1   3 
One more   2   3 
+0

可能重複[如何打印列表中的控制檯應用程序表?](HTTP:/ /stackoverflow.com/questions/10160770/how-to-print-list-as-table-in-console-application) – nunzabar

回答

2

檢查出Console.WriteLine,在那裏你會看到它使用了composite formatting feature,支持定位參數的文檔。所以,你可以使用對齊的東西,例如

Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{column 1}", "{column 2}", "{column 3}") 
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "This is some data", 0, 3) 

導致:

{column 1}   {column 2} {column 3} 
This is some data 0   3 

調整格式字符串的間距和路線會得到你想要的東西。

1

如果你希望用戶能夠手動數據輸入到一個表:

Console.Write("Enter Data For Column 1: ") 
    Dim Data1 As String = Console.ReadLine 
    Console.Write("Enter Data For Column 2: ") 
    Dim Data2 As String = Console.ReadLine 

    Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{Data Type}", "{Column 1}", "{Column 2}") 
    Console.WriteLine("{0,-20} {1,-10} {2,-10}", "Data Entered:", Data1, Data2) 

    Console.WriteLine("ENTER To Exit: ") 
    Console.ReadLine() 

It should look like this (Click Me).