有没有用于绘制 ASCII 表格的 Java 库?
我需要将数据作为表格输出到控制台。我想知道也许有一些 java 库可以处理 ASCII 艺术中的绘图表格、对齐单元格内的值等?
╔══════╤═══════════╤════════╗
║ ID │ Name │ Age ║
╠══════╪═══════════╪════════╣
║ 1 │ John │ 24 ║
╟──────┼───────────┼────────╢
║ 2 │ Jeff │ 19 ║
╟──────┼───────────┼────────╢
║ 3 │ Joel │ 42 ║
╚══════╧═══════════╧════════╝
I need to output data into a console as a table. I was wondering maybe there are some java libraries that would take care of drawing tables in ASCII art, aligning values inside cells, etc?
╔══════╤═══════════╤════════╗
║ ID │ Name │ Age ║
╠══════╪═══════════╪════════╣
║ 1 │ John │ 24 ║
╟──────┼───────────┼────────╢
║ 2 │ Jeff │ 19 ║
╟──────┼───────────┼────────╢
║ 3 │ Joel │ 42 ║
╚══════╧═══════════╧════════╝
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这对我来说效果很好:http://code.google.com/p/java-ascii-table/
呈现以下内容:
This worked pretty well for me: http://code.google.com/p/java-ascii-table/
Which renders the following:
尝试适用于 Java 的 iNamik 文本表格式化程序。
Try iNamik Text Table Formatter for Java.
我喜欢你的桌子并写道:
https://github.com/klaus31/ascii-art-table
I like your table and wrote that:
https://github.com/klaus31/ascii-art-table
这里还有一个方便的库: https://github.com/JakeWharton/flip-tables
作为文档说:
应该有以下输出:
Here is also a handy library: https://github.com/JakeWharton/flip-tables
As the doc said:
should have the following output:
您可以尝试文本表格式化程序 。它不是一个完美的库:它没有 JavaDocs,没有用户指南,源代码包含拼写错误(例如,请参阅包含
HEADER_AND_FIRST_COLLUMN
的 ShownBorders 类) > 常数)。甚至此项目页面也包含示例的用法至少有一个拼写错误(HorizontalAlign.right
而不是HorizontalAlign.RIGHT
)。但是,它位于 Maven 中央存储库中(与 iNamik 的文本表格式化程序不同),非常灵活,易于使用,而且有效。这是库创建者提供的固定“高级”示例输出:
另一个选项(我个人更喜欢它)是 ASCII 表。与之前的库不同,它有一个很好的用户指南,JavaDocs,以及 Maven Central 中的多个版本(这意味着它被维护,至少有一段时间,最后一次是2017年5月)。这是一个可以用它做什么的示例(我将省略类和主方法声明)
输出:
You can try Text Table Formatter. It's not a perfect library: it doesn't have JavaDocs, it doesn't have a user guide, the source code contains typos (e.g. see the
ShownBorders
class that contains aHEADER_AND_FIRST_COLLUMN
constant). Even this project page that contains examples of usages has at least one typo (HorizontalAlign.right
instead ofHorizontalAlign.RIGHT
). However, it's in the Maven Central repository (unlike iNamik's Text Table Formatter), pretty flexible, easy to use, and it works. Here's the fixed "advanced" example provided by the library's creatorsOutput:
Another option (I personally like it more) is ASCII Table. Unlike the previous library, it has a good user guide, JavaDocs, and multiple versions in Maven Central (which implies it was maintained, at least for a while, the last one is of May 2017). Here's an example of what you can do with it (I'll omit the class and main method declarations)
Output:
如果您已经有一个具有所需列宽的格式化二维字符串数组,那么您可以自己绘制一个简单的表格,无需任何库,如下所示:
尝试一下在线!
另请参阅:
• 如何用Java绘制楼梯?
• 格式化二维数字数组
If you already have a formatted 2d array of strings with the desired column widths, then you can draw a simple table yourself without any libraries, something like this:
Try it online!
See also:
• How to draw a staircase with Java?
• Formatting 2d array of numbers