有没有用于绘制ASCII表格的Java库?

21

我需要将数据以表格形式输出到控制台。我想知道是否有一些Java库可以绘制ASCII艺术表格,对齐单元格内的值等等?

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

4
如果你想要严格遵守ASCII艺术中的ASCII标准,那么你只能使用+-|作为线条绘制字符,而不能使用你在示例中使用的花哨字符。 - Roland Illig
我曾经在PHP中创建过这样一个函数(用于格式化电子邮件输出的表格)。它与其他输出到HTML或PDF的函数具有相同的接口。 - Paŭlo Ebermann
这里是TekstaKotizoFormatilo类的链接(http://svn.berlios.de/wsvn/aligilo/programo/iloj/iloj_kotizo_formatado.php)。但是它没有太多注释,而且是用世界语编写的,适用于我们自己的表格式,因此可能对您没有帮助。如果在此找不到合适的库,我可能会考虑将其移植到Java。 - Paŭlo Ebermann
1
嗯...为什么谷歌翻译不支持世界语呢? - Vladimir Dyuzhev
1
1980年代打来电话了……他们要回收他们的字符集。 - WhiteFang34
https://github.com/MarounMaroun/simple-table - Maroun
6个回答

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"},
};

这会产生以下内容:
+-----------+--------+-------------+------------------------+---------+
| 用户名    | 薪水   | 职位        | 地址                   | 幸运号   |
+-----------+--------+-------------+------------------------+---------+
|       Ram |   2000 |     经理    |        #99, Silk board |    1111 |
|       Sri |  12000 |   开发人员  |             BTM Layout |   22222 |
|    Prasad |  42000 |        领导 | #66, Viaya Bank Layout |  333333 |
|       Anu | 132000 |          QA |             #22, Vizag | 4444444 |
|       Sai |  62000 |   开发人员  |         #3-3, Kakinada |         |
|    Venkat |   2000 |     经理    |                        |         |
|       Raj |  62000 |             |                        |         |
|       BTC |        |             |                        |         |
+-----------+--------+-------------+------------------------+---------+


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    ║
╚══════╧════════╝

3

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绘制楼梯?
格式化数字的二维数组

0
你可以试试Text Table Formatter。这个库并不完美:它没有JavaDocs,也没有用户指南,源代码中包含有拼写错误(例如,查看包含一个HEADER_AND_FIRST_COLLUMN常量的ShownBorders类)。甚至这个项目页面上展示的用法示例中也至少有一个拼写错误(HorizontalAlign.right而不是HorizontalAlign.RIGHT)。然而,它在Maven Central仓库中(与iNamik's Text Table Formatter不同),非常灵活、易于使用,并且它能正常工作。以下是该库创建者提供的修复后的“高级”示例。
public class Advanced {

  public static void main(final String[] args) {

    CellStyle numberStyle = new CellStyle(HorizontalAlign.RIGHT);

    Table t = new Table(3, BorderStyle.DESIGN_FORMAL,
        ShownBorders.SURROUND_HEADER_FOOTER_AND_COLUMNS);
    t.setColumnWidth(0, 8, 14);
    t.setColumnWidth(1, 7, 16);
    t.setColumnWidth(2, 9, 16);
    
    t.addCell("Region");
    t.addCell("Orders", numberStyle);
    t.addCell("Sales", numberStyle);

    t.addCell("North");
    t.addCell("6,345", numberStyle);
    t.addCell("$87.230", numberStyle);

    t.addCell("Center");
    t.addCell("837", numberStyle);
    t.addCell("$12.855", numberStyle);

    t.addCell("South");
    t.addCell("5,344", numberStyle);
    t.addCell("$72.561", numberStyle);

    t.addCell("Total", numberStyle, 2);
    t.addCell("$172.646", numberStyle);

    System.out.println(t.render());
  }

}

输出:

==========================
Region    Orders     Sales
-------- ------- ---------
North      6,345   $87.230
Center       837   $12.855
South      5,344   $72.561
-------- ------- ---------
           Total  $172.646
==========================

另一个选择(我个人更喜欢)是ASCII表。与之前的库不同,它有一个很好的用户指南JavaDocs,以及Maven Central上的多个版本(这意味着它至少在一段时间内得到了维护,最后一个版本是2017年5月)。以下是使用它的示例(我将省略类和主方法的声明)。
        AsciiTable table = new AsciiTable();
        table.getContext().setGrid(A8_Grids.lineDobuleTripple());
        table.addHeavyRule();
        table.addRow(null, null, "Countries");
        table.addHeavyRule();
        table.addRow("Country", "Capital", "Population");
        table.addRule();
        table.addRow("United States", "Washington", "333,287,557");
        table.addRow("United Kingdom", "London", "68,138,484");
        table.addRow("Australia", "Canberra", "26,540,400");
        table.addHeavyRule();
        System.out.println(table.render());

输出:

≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
 Countries                                                                      
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
 Country                    Capital                   Population                
────────────────────────────────────────────────────────────────────────────────
 United States              Washington                333,287,557               
 United Kingdom             London                    68,138,484                
 Australia                  Canberra                  26,540,400                
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接