生成 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:51:16  来源:igfitidea点击:

Generating MS Word reports and export uing java

apache-poijava

提问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 .docformat. docxformat 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:

所以,任务是:

  • create tables in MS Word documents
  • 在 MS Word 文档中创建表格
  • insert subtables into table cells
  • 将子表插入表格单元格
  • merge table cells and rows
  • 合并表格单元格和行
  • replace text in documents leaving the formatting

    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 POIAspose库来完成这项工作。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"));
    

    所以到那时我有三个问题:

  • Am I doing something wrong in this example?
  • 在这个例子中我做错了什么吗?
  • Is POI applicable for the tasks I mentioned?
  • POI 是否适用于我提到的任务?
  • What other freelibraries you can suggest

    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:

    有两种方法可以做到这一点:

    1. This I really recommend since this solves your problem of replacing template placeholders with actual values, easily:
    1. 这是我真正推荐的,因为这可以轻松解决您用实际值替换模板占位符的问题:

    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

    1. Look at docx4j.
    1. 看看 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:

    你也可以看看: