c# - 如何在控制台应用程序中将列表打印为表格?

标签 c# .net

<分区>

我需要方法在控制台应用程序中将列表打印为表格并以这样方便的格式预览:

Pom_No          Item_Code          ordered_qty                      received_qty

1011            Item_Code1         ordered_qty1                    received_qty1 

1011            Item_Code2         ordered_qty2                    received_qty2

1011            Item_Code3         ordered_qty3                    received_qty3

1012            Item_Code1         ordered_qty1                    received_qty1 

1012            Item_Code2         ordered_qty2                    received_qty2

1012            Item_Code3         ordered_qty3                    received_qty3

最佳答案

你的主要工具是

Console.WriteLine("{0,5} {1,10} {2,-10}", s1, s2, s3);  

,5,10 是宽度说明符。使用负值左对齐。

也可以格式化:

Console.WriteLine("y = {0,12:#,##0.00}", y);

或者宽度为 24 和自定义格式的日期:

String.Format("Now = {0,24:dd HH:mm:ss}", DateTime.Now);

为 C#6 编辑

有了字符串插值你现在可以写

Console.WriteLine($"{s1,5} {s2,10} {s3,-10}");  
Console.WriteLine($"y = {y,12:#,##0.00}");

您不再需要显式调用 String.Format():

string s = $"Now = {DateTime.Now,24:dd HH:mm:ss}, y = {y,12:#,##0.00}" ;

关于c# - 如何在控制台应用程序中将列表打印为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160770/

相关文章:

c# - 如何消除 WPF 输出窗口中的绑定(bind) “Information” - 无法使用绑定(bind)检索值

c# - 异步命令和 Task.WhenAny 在 StackExchange.Redis 中等待后出现超时异常

c# - MySQL 连接器在枚举后面附加比实际值小一的值

c# - 从 Asp.Net Mvc 中的数据库下载文件

javascript - DataTables 警告 - 为第 0 行、第 0 列请求未知参数 '0'

c# - MVVM 最佳实践 : communication between view models

c# - 继续 Task<T> 时保留异常

c# - 将 AdWords XML 文件转换为 C# 列表

c# - 在 LINQ 中使用 MIN 函数

c# - RavenDB 服务器模式性能