如何从数据库中整齐地打印表

时间:2019-05-20 16:42:18

标签: wpf

我尝试从数据库中打印表格,但是它以不规则的方式打印

它看起来像这样:

text text text text

text text   text  text

text text     text...............
conn.Open();
SqlDataReader thereader = com.ExecuteReader();

while (thereader.Read())
{
    for (int i = 0; i < thereader.FieldCount; i++)
    {
        DataApear.Text += thereader.GetValue(i)+"                   ";
    }
    DataApear.Text += "\n";
}
conn.Close();

1 个答案:

答案 0 :(得分:0)

您可以像这样(来自String.Format docs)使用String.Format中的Alignment Component:

int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population");
for(int index = 0; index < years.Length; index++)
   s += String.Format("{0,-10} {1,-10:N0}\n",
                  years[index], population[index]);
Console.WriteLine($"\n{s}");
// Result:
//    Year       Population
//
//    2013       1,025,632
//    2014       1,105,967
//    2015       1,148,203