如何将表格数据格式化为Java中的文本? [英] How to format tabular data as text in Java?

查看:143
本文介绍了如何将表格数据格式化为Java中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从任意数据集对象模型中生成格式良好的表格文本。有没有一个好的库在Java中做到这一点?



具体来说,我想输出格式化为像命令行数据管理工具,如MySQL的CLI。示例:

  + --------- + ------------- -  + ------ + ----- + --------- + ------- + 
|字段|类型|空|密钥|默认|额外|
+ --------- + -------------- + ------ + ----- + ------- - + ------- +
|名字| varchar(100)|是| | NULL | |
|释放|年(4)|是| | NULL | |
|工作室| varchar(50)|是| | NULL | |
|审查| varchar(50)|是| | NULL | |
|毛| | int(11)|是| | NULL | |
+ --------- + -------------- + ------ + ----- + ------- - + ------- +

一个主要的挑战是我不会自动在开始遍历数据之前知道最大列宽。另外,还有很多边缘情况,例如处理非常大的值长度和大量的行和列。如果我必须自己构建这个,我想象我会利用String.format,我需要预先分析完整的数据集,然后再开始输出。这是一个非常低的编码水平,所以我很乐意找到一个好的库,已经解决了这个问题。

解决方案

查看 TableFormatter :它似乎是做你想要的格式化表格数据。源代码也是可用的。您可以使用流畅的界面方法将行和单元格追加到表格中。下面是一个例子:

pre $ Table $ $ $ $ $ $ $ $ $
.nextCell()
.addLine(Field)
.nextCell()
.addLine(Type)
/ pre>

因此,它会以正确的格式建立单元格并相应地调整空格。它也将允许你做单元格(中心,顶部,底部)
的文本基本对齐

I would like to produce nicely formatted tabular text from arbitrary dataset object models. Is there a good library to do this in Java?

Specifically, I want output that is formatted like command line data management tools such as the CLI for mysql. Example:

+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(100) | YES  |     | NULL    |       |
| release | year(4)      | YES  |     | NULL    |       |
| studio  | varchar(50)  | YES  |     | NULL    |       |
| review  | varchar(50)  | YES  |     | NULL    |       |
| gross   | int(11)      | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+

One main challenge is that I won't automatically know the maximum column widths before I start traversing the data. Also, there are plenty of edge cases, such as dealing with very large value lengths and large numbers of rows and columns.

If I have to build this myself, I imagine I would make use of String.format, and I'd need to pre-analyze the full dataset before starting the output. That's a very low level of coding though, so I'd love to find a good library that has already solved this problem.

解决方案

Check out TableFormatter: it appears to do what you want for formatting tabular data. Source code is also available. You can append rows and cells to a table using a fluent interface approach. An example follows:

TableFormatter tf = new SimpleTableFormatter(true) // true = show border
        .nextRow()
            .nextCell()
                .addLine("Field")
            .nextCell()
                .addLine("Type")

So it will build up the cells in the correct format and adjust whitespace accordingly. It also will allow you to do basic alignment of text within the cell (center, top, bottom)

这篇关于如何将表格数据格式化为Java中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆