20

我需要将数据作为表格输出到控制台。我想知道也许有一些 java 库可以处理 ASCII 艺术中的绘图表、对齐单元格内的值等?

 ╔══════╤═══════════╤════════╗
 ║  ID  │ Name      │  Age   ║ 
 ╠══════╪═══════════╪════════╣
 ║  1   │ John      │   24   ║ 
 ╟──────┼───────────┼────────╢
 ║  2   │ Jeff      │   19   ║ 
 ╟──────┼───────────┼────────╢
 ║  3   │ Joel      │   42   ║ 
 ╚══════╧═══════════╧════════╝
4

5 回答 5

7

这对我来说效果很好:http ://code.google.com/p/java-ascii-table/

String [] header = {
      "User Name", 
      "Salary", "Designation",
      "Address", "Lucky#"
};

String[][] data = {
      { "Ram", "2000", "Manager", "#99, Silk board", "1111"  },
      { "Sri", "12000", "Developer", "BTM Layout", "22222" },
      { "Prasad", "42000", "Lead", "#66, Viaya Bank Layout", "333333" },
      { "Anu", "132000", "QA", "#22, Vizag", "4444444" },
      { "Sai", "62000", "Developer", "#3-3, Kakinada"  },
      { "Venkat", "2000", "Manager"   },
      { "Raj", "62000"},
      { "BTC"},
};

呈现以下内容:

+-----------+--------+-------------+-------------- ----------+---------+
| 用户名 | 工资 | 名称 | 地址 | 幸运# |
+-----------+--------+-------------+-------------- ----------+---------+
| 拉姆 | 2000 | 经理 | #99,丝绸板 | 1111 |
| 斯里兰卡 | 12000 | 开发商 | BTM 布局 | 22222 |
| 普拉萨德 | 42000 | 铅 | #66,维亚亚银行布局| 333333 |
| 阿努 | 132000 | 质量保证 | #22,维扎格 | 4444444 |
| 赛 | 62000 | 开发商 | #3-3, Kakinada | |
| 文卡特 | 2000 | 经理 | | |
| 拉吉 | 62000 | | | |
| 比特币 | | | | |
+-----------+--------+-------------+-------------- ----------+---------+
于 2012-11-28T18:07:55.350 回答
4

试试iNamik Text Table Formatter for Java

于 2011-04-10T04:12:08.600 回答
3

我喜欢你的桌子并写道: https ://github.com/klaus31/ascii-art-table

于 2015-04-15T18:25:02.077 回答
3

这里也是一个方便的库:https ://github.com/JakeWharton/flip-tables

正如医生所说:

String[] headers = { "Test", "Header" };
String[][] data = {
    { "Foo", "Bar" },
    { "Kit", "Kat" },
};
System.out.println(FlipTable.of(headers, data));

应该有以下输出:

╔══════╤════════╗
║ Test │ Header ║
╠══════╪════════╣    
║ Foo  │ Bar    ║
╟──────┼────────╢
║ Kit  │ Kat    ║
╚══════╧════════╝
于 2016-05-02T20:43:22.687 回答
0

如果您已经有一个具有所需列宽的格式化二维字符串数组,那么您可以自己绘制一个简单的表格,无需任何库,如下所示:

+------+---------+-------+
|  ID  |  Name   |  Age  |
+------+---------+-------+
|   1  |  John   |   24  |
+------+---------+-------+
|   2  |  Jeff   |   19  |
+------+---------+-------+
|   3  |  Joel   |   42  |
+------+---------+-------+

在线尝试!

public static String drawTable(String[][] table) {
    String borderRow = Arrays.stream(table[0])
            // border row between rows
            .map(str -> "-".repeat(str.length()))
            .collect(Collectors.joining("+", "+", "+\n"));
    return Arrays.stream(table)
            // table row with borders between cells
            .map(row -> Arrays.stream(row)
                    .collect(Collectors.joining("|", "|", "|\n")))
            .collect(Collectors.joining(borderRow, borderRow, borderRow));
}
public static void main(String[] args) {
    String[][] table = {
            {"  ID  ", "  Name   ", "  Age  "},
            {"   1  ", "  John   ", "   24  "},
            {"   2  ", "  Jeff   ", "   19  "},
            {"   3  ", "  Joel   ", "   42  "}};

    System.out.println(drawTable(table));
}

另请参阅:
如何使用 Java 绘制楼梯?
格式化二维数字数组

于 2021-04-11T11:17:41.837 回答