java Java以文本文件格式生成格式化报告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13813247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Generating Formatted Report in Text file format
提问by coldholic
Halo,
光环,
I have been looking for a way to generate a nicely formatted report in text file using java.
我一直在寻找一种使用 java 在文本文件中生成格式良好的报告的方法。
For example, I might need to print a report in the following format
例如,我可能需要按以下格式打印报告
A Monthly Report A Report Name Page No: 1 A Date: YYYY-MM-DD A A Category Quantity Price A ----------------- ----------------- -------------------- B Pen 100 0 B Paper 200 0 A A ================= ==================== B Total 0 A ================= ====================
I have tried writing my own program, but I just feel that its a mess!!! So I am wondering if there are any existing library that I can use or is there a good way to implement it??
我曾尝试编写自己的程序,但我只是觉得它很烂!!!所以我想知道是否有任何现有的库可以使用,或者是否有实现它的好方法?
By the way, I have look around and found a library that are similar to what I want https://github.com/iNamik/Java-Text-Table-Formatter
顺便说一句,我环顾四周,发现了一个与我想要的类似的库 https://github.com/iNamik/Java-Text-Table-Formatter
Just wondering if there are other options. Thanks for helping!!
只是想知道是否还有其他选择。谢谢帮忙!!
====================================================================
================================================== ==================
So I have made a sample code that I probably will use to clean up my code
所以我制作了一个示例代码,我可能会用它来清理我的代码
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s %50s%n", "A", "Monthly Report"));
sb.append(String.format("%s %48s%n", "A", "Report Name"));
sb.append(String.format("%s %n", "A"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "Category", "Quantity", "Price"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "--------------", "--------------", "--------------"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "B", "Paper", 100, "0"));
System.out.println(sb.toString());
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s %50s%n", "A", "Monthly Report"));
sb.append(String.format("%s %48s%n", "A", "Report Name"));
sb.append(String.format("%s %n", "A"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "Category", "Quantity", "Price"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "A", "--------------", "--------------", "--------------"));
sb.append(String.format("%s %-20s %-20s %-20s%n", "B", "Paper", 100, "0"));
System.out.println(sb.toString());
Output:
输出:
A Monthly Report A Report Name A A Category Quantity Price A -------------- -------------- -------------- B Paper 100 0
I am thinking how can I make the "Report Name" at the center and "Page No:" at the right without hard coding the int argument of the formatter (i.e. %50s, without 50, is it possible)
我在想如何在不硬编码格式化程序的 int 参数的情况下在中心设置“报告名称”和在右侧设置“页码:”(即 %50s,没有 50,是否可能)
采纳答案by Evgeniy Dorofeev
Just as an alternative, here is JDK based solution
作为替代方案,这里是基于 JDK 的解决方案
public static void main(String[] args) throws Exception {
printRow("A", "Category", "Quantity", "Price");
printRow("A", "--------------", "--------------", "--------------");
printRow("B", "Paper", 100, 200);
}
private static void printRow(String c0, String c1, Object c2, Object c3 ) {
System.out.printf("%s %-20s %-20s %-20s%n", c0, c1, String.valueOf(c2), c3 instanceof Integer ? "$" + c3 : c3);
}
output
输出
A Category Quantity Price
A -------------- -------------- --------------
B Paper 100 0
回答by CarlosMecha
Apache Velocityis a good tool for text formating or templating. It works with plain text, HTML, JSP, XML, SQL scripts, etc. Here is a nice helloWorld tutorialabout it.
Apache Velocity是一个很好的文本格式化或模板工具。它适用于纯文本、HTML、JSP、XML、SQL 脚本等。这是一个很好的 helloWorld教程。
The basic steps are:
基本步骤是:
- Write your text template.
- Initialize the Velocity Engine.
- Insert the context that you need.
- And render it.
- 编写您的文本模板。
- 初始化速度引擎。
- 插入您需要的上下文。
- 并渲染它。
Others like Latexare more complex but really more powerful. Take a look to JasperReportsif you need just reporting formats.
其他如Latex更复杂,但确实更强大。如果您只需要报告格式,请查看JasperReports。
回答by Nitesh Verma
Try iText java library i have used it for generating bills for my application.
试试 iText java 库,我用它来为我的应用程序生成账单。