你可以试试
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
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
+
、-
和|
作为线条绘制字符,而不能使用你在示例中使用的花哨字符。 - Roland Illig