控制台窗口上的VB.NET打印格式化表

时间:2014-04-29 16:27:39

标签: 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

2 个答案:

答案 0 :(得分:3)

查看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 :(得分: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).