How can I align text in columns using Console.WriteLine?

前端 未结 8 1246
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 22:11

I have a sort of column display, but the end two column\'s seem to not be aligning correctly. This is the code I have at the moment:

Console.WriteLine(\"Cust         


        
8条回答
  •  星月不相逢
    2020-11-28 22:43

    There're several NuGet packages which can help with formatting. In some cases the capabilities of string.Format are enough, but you may want to auto-size columns based on content, at least.

    ConsoleTableExt

    ConsoleTableExt is a simple library which allows formatting tables, including tables without grid lines. (A more popular package ConsoleTables doesn't seem to support borderless tables.) Here's an example of formatting a list of objects with columns sized based on their content:

    ConsoleTableBuilder
        .From(orders
            .Select(o => new object[] {
                o.CustomerName,
                o.Sales,
                o.Fee,
                o.Value70,
                o.Value30
            })
            .ToList())
        .WithColumn(
            "Customer",
            "Sales",
            "Fee",
            "70% value",
            "30% value")
        .WithFormat(ConsoleTableBuilderFormat.Minimal)
        .WithOptions(new ConsoleTableBuilderOption { DividerString = "" })
        .ExportAndWriteLine();
    

    CsConsoleFormat

    If you need more features than that, any console formatting can be achieved with CsConsoleFormat.† For example, here's formatting of a list of objects as a grid with fixed column width of 10, like in the other answers using string.Format:

    ConsoleRenderer.RenderDocument(
        new Document { Color = ConsoleColor.Gray }
            .AddChildren(
                new Grid { Stroke = LineThickness.None }
                    .AddColumns(10, 10, 10, 10, 10)
                    .AddChildren(
                        new Div("Customer"),
                        new Div("Sales"),
                        new Div("Fee"),
                        new Div("70% value"),
                        new Div("30% value"),
                        orders.Select(o => new object[] {
                            new Div().AddChildren(o.CustomerName),
                            new Div().AddChildren(o.Sales),
                            new Div().AddChildren(o.Fee),
                            new Div().AddChildren(o.Value70),
                            new Div().AddChildren(o.Value30)
                        })
                    )
            ));
    

    It may look more complicated than pure string.Format, but now it can be customized. For example:

    • If you want to auto-size columns based on content, replace AddColumns(10, 10, 10, 10, 10) with AddColumns(-1, -1, -1, -1, -1) (-1 is a shortcut to GridLength.Auto, you have more sizing options, including percentage of console window's width).

    • If you want to align number columns to the right, add { Align = Right } to a cell's initializer.

    • If you want to color a column, add { Color = Yellow } to a cell's initializer.

    • You can change border styles and more.

    † CsConsoleFormat was developed by me.

提交回复
热议问题