使用 Java 生成 PowerPoint 2007/2010 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2152702/
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
Generate PowerPoint 2007/2010 file using Java
提问by sreeprasad
Does anyone know of any API (commercial or open-source) that can generate/edit PowerPoint 2007/2010 presentations through Java. I have a template in the PowerPoint 2007/2010 format that I require to edit/update. So far I have been converting the .pptx file to xml and then editing and storing it back as .pptx. But the file gets corrupted while opening.
有谁知道可以通过 Java 生成/编辑 PowerPoint 2007/2010 演示文稿的任何 API(商业或开源)。我有一个 PowerPoint 2007/2010 格式的模板,需要编辑/更新。到目前为止,我一直在将 .pptx 文件转换为 xml,然后将其编辑并存储为 .pptx。但是文件在打开时已损坏。
Is anyone aware of any other method or API that do this in Java?
有没有人知道在 Java 中执行此操作的任何其他方法或 API?
回答by James
Yes. Check this out http://poi.apache.org/, they just released version 3.6 which now supports Office 2007 format documents. The best part is that it's free!
是的。查看http://poi.apache.org/,他们刚刚发布了 3.6 版,现在支持 Office 2007 格式的文档。最好的部分是它是免费的!
回答by JasonPlutext
回答by GrantB
We have done it programmatically (closed source at the moment, sorry) so might be able to help, but beware of a few gotchas.
我们以编程方式完成了它(目前是封闭源代码,抱歉)所以可能会有所帮助,但要注意一些问题。
One is that the POI project (at least when we looked at it last year), was quite incomplete. It didn't do PPTX Charts - which is the one feature we wanted. Infact the POI site may not be upto date, but they don't appear to support PowerPoint 20087 format (http://poi.apache.org/slideshow/index.html). Everybody recommends this project, but our evaluation was that it was pretty much useless for generating PowerPoint 2007 files via Java. Your milage may vary.
一个是 POI 项目(至少当我们去年查看它时)非常不完整。它没有做 PPTX 图表——这是我们想要的一项功能。事实上,POI 站点可能不是最新的,但它们似乎不支持 PowerPoint 20087 格式 ( http://poi.apache.org/slideshow/index.html)。每个人都推荐这个项目,但我们的评价是它对于通过 Java 生成 PowerPoint 2007 文件几乎没有用。您的里程可能会有所不同。
Apose also had some significant limitations when we looked at it; not doing Charts in PowerPoint 2007 being the blocking issue for us.
当我们查看它时,Apose 也有一些明显的局限性;在 PowerPoint 2007 中不做图表是我们的阻塞问题。
Another issue is that PowerPoint 2007 can be quite buggy. We have had a number of progammatically produced PPT files that caused lock ups, but when testing, we found that we can repro crashes and lock ups with simple PPTX documents created in PowerPoint 2007 - i.e. not our code.
另一个问题是 PowerPoint 2007 可能有很多问题。我们有许多程序生成的 PPT 文件导致锁定,但在测试时,我们发现我们可以使用在 PowerPoint 2007 中创建的简单 PPTX 文档重现崩溃和锁定 - 即不是我们的代码。
In the end, we did the following: Unpacked a 'template' PowerPoint file to a folder, then on demand, filled the template XML with new values, zipped it up, renaming various elements & delivered it to the user as a valid PPTX. Works OK, other than the odd PowerPoint crash when people edit the file. If there was a market for it, I guess we could package up the code as a webservice (i.e xml/csv -> PPTX) or put together a commerical package, but we wouldn't do it for free.
最后,我们执行了以下操作:将“模板”PowerPoint 文件解压缩到一个文件夹中,然后根据需要,用新值填充模板 XML,将其压缩,重命名各种元素并将其作为有效的 PPTX 交付给用户。工作正常,除了人们编辑文件时奇怪的 PowerPoint 崩溃。如果有市场,我想我们可以将代码打包为网络服务(即 xml/csv -> PPTX)或组合一个商业包,但我们不会免费做。
回答by amanteaux
To generate a PowerPoint presentation from a template file, you can use PPT Templates.
要从模板文件生成 PowerPoint 演示文稿,您可以使用PPT 模板。
This library provides a fluent API to replace variables inside the PPT template:
这个库提供了一个流畅的 API 来替换 PPT 模板中的变量:
try(FileOutputStream out = new FileOutputStream("generated.pptx")) {
new PptMapper()
.text("variable", "Hello")
.text("other_variable", "World!")
.processTemplate(PptTemplateDemo.class.getResourceAsStream("/title.pptx"))
.write(out);
}
With this library, you can process text and images in the template.
使用此库,您可以处理模板中的文本和图像。
回答by David Thielen
Another solution that may work for you is Windward Reports(disclaimer, I'm the founder & CEO there). It uses PPTX as one of the supported template formats and merges in data to then generate a PPTX (or PDF, etc.) output.
另一种可能对您有用的解决方案是Windward Reports(免责声明,我是那里的创始人兼首席执行官)。它使用 PPTX 作为支持的模板格式之一并合并数据以生成 PPTX(或 PDF 等)输出。
If the edit/update you need can be handled via the data tags in Windward, this should be trivial for you. If what you need cannot be handled by the tags, then this won't work for you.
如果您需要的编辑/更新可以通过 Windward 中的数据标签处理,这对您来说应该是微不足道的。如果标签无法处理您需要的内容,那么这对您不起作用。
回答by sayannayas
Well as mentioned by GrantB best way is to create a template
, then load the template
, traverse the xml graph
,update the data and stream out to a output ppt. We recently did it to generate reports for clients that had complex visuals and charts in ppt. You can have a look here generate ppt in java
以及由GrantB提到的最好办法是create a template
,那么load the template
,traverse the xml graph
中,更新数据和流输出到输出百分点。我们最近这样做是为了为 ppt 中具有复杂视觉效果和图表的客户生成报告。你可以看看这里用java生成ppt