生成 MS Word 报告并使用 java 导出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901448/
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
Generating MS Word reports and export uing java
提问by kant
I need to export tables and generate reports from my application using pure java. Using COM or anything that needs pre-installed applications is not allowed. I definitely need .doc
format. docx
format is optional.
我需要使用纯 Java 从我的应用程序导出表并生成报告。不允许使用 COM 或任何需要预安装应用程序的东西。我绝对需要.doc
格式。docx
格式是可选的。
To exporttable means just to create simple table in word document with data. To generate reportis to replace placeholders with some values in a template table. It also involves inserting new subtables in a template table or merging cells and rows.
要导出表的手段只是为了数据的Word文档中创建简单的表。生成报告就是用模板表中的一些值替换占位符。它还涉及在模板表中插入新的子表或合并单元格和行。
So, the task is:
所以,任务是:
I've tried to search the net but I managed to find Apache POIand Asposelibraries to do the job. Aspose seems to be ok but unfortunately I can't afford this. POI has very poor documentation and I can't really figure out whether it suits or not.
Moreover I tried to insert simple table into the document. But it just corrups document. Check out the code below:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("poi.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Table table = range.insertTableBefore((short)5, 5);
table.getRow(0).getCell(0).replaceText("cell", true);
doc.write(new FileOutputStream("poi_out.doc"));
So i have three questions by that moment:
我试图在网上搜索,但我设法找到了Apache POI和Aspose库来完成这项工作。Aspose 似乎还可以,但不幸的是我买不起。POI 的文档很差,我真的不知道它是否适合。
此外,我尝试在文档中插入简单的表格。但它只会破坏文件。查看下面的代码:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("poi.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Table table = range.insertTableBefore((short)5, 5);
table.getRow(0).getCell(0).replaceText("cell", true);
doc.write(new FileOutputStream("poi_out.doc"));
所以到那时我有三个问题:
Thanks for your replies
P.S.I've read some posts on this site about that topic. None of them really helped me. Moreover all of them are old. Some new features probably changed since that time.
感谢您的回复
PS我已经阅读了本网站上有关该主题的一些帖子。他们都没有真正帮助过我。而且,他们都老了。从那时起,一些新功能可能发生了变化。
回答by sethu
There are 2 ways you can do this:
有两种方法可以做到这一点:
- This I really recommend since this solves your problem of replacing template placeholders with actual values, easily:
- 这是我真正推荐的,因为这可以轻松解决您用实际值替换模板占位符的问题:
Create a HTML file with the stuff you need. Use velocity as your templating engine. Now, after you replace the placeholders with values in your code, save the file with an extension of '.doc' or '.docx'. The resultant file will open in word nicely. It will open in the web layout and the user can switch to the print layout if he likes.
用你需要的东西创建一个 HTML 文件。使用速度作为模板引擎。现在,在用代码中的值替换占位符后,使用“.doc”或“.docx”扩展名保存文件。生成的文件将在 word 中很好地打开。它将在 Web 布局中打开,用户可以根据需要切换到打印布局。
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
- Look at docx4j.
- 看看 docx4j。
http://www.docx4java.org/trac/docx4j
http://www.docx4java.org/trac/docx4j
But for this you will have understand how the MS Office Word docs XML structure works. And it might be really complicated putting a table inside another table.
但为此,您将了解 MS Office Word 文档 XML 结构的工作原理。把一张桌子放在另一个桌子里面可能真的很复杂。
回答by Paul Jowett
You might want to look at JODReportswhich is free, can do doc/docx. It's probably faster than POI but does require OpenOffice/LibreOffice to be installed somewhere. Docmosisis another tool which can do this and you can use it without installing anything if you are able to call the cloud service (which is pretty cheap but not free). I don't know what your problem is with the POI code example, but POI can produce doc documents (I'm not sure about docx) and it is free.
你可能想看看JODReports,它是免费的,可以做 doc/docx。它可能比 POI 更快,但确实需要在某处安装 OpenOffice/LibreOffice。 Docmosis是另一种可以做到这一点的工具,如果您能够调用云服务(非常便宜但不是免费的),则无需安装任何东西即可使用它。我不知道您的 POI 代码示例有什么问题,但是 POI 可以生成 doc 文档(我不确定 docx)并且它是免费的。
回答by Cléssio Mendes
You can also have a look at:
你也可以看看:
- Templater(commercial licenses apply)
- XDocReport(completely open source)
- 模板程序(适用商业许可证)
- XDocReport(完全开源)