OpenOffice,将文档写入 servlet 响应
目前,我们使用 OpenOffice 来获取模板文件文档中的书签,并通过 Java 将其替换为数据库中的内容。实际保存文件的代码行看起来像这样...
XStorable storable = UnoRuntime.queryInterface(XStorable.class, document);
// Save as Word 97 Document
PropertyValue[] properties = new PropertyValue[1];
PropertyValue property = new PropertyValue();
property.Name = "FilterName";
property.Value = FORMAT_WORD_97;
properties[0] = property;
storable.storeAsURL(saveFileURL, properties);
我们想直接将文件写入 servlet 响应输出流,有人知道通过 OpenOffice 的 UNO api 直接以字节数组或输入流形式获取文档的方法吗爪哇?
Currently we use OpenOffice to grab bookmarks in a template file document and replace them with content from our DB via Java. The lines of code that actually save the file look like this...
XStorable storable = UnoRuntime.queryInterface(XStorable.class, document);
// Save as Word 97 Document
PropertyValue[] properties = new PropertyValue[1];
PropertyValue property = new PropertyValue();
property.Name = "FilterName";
property.Value = FORMAT_WORD_97;
properties[0] = property;
storable.storeAsURL(saveFileURL, properties);
We want to directly write the file to the servlet response outputstream, does anybody know of a way to directly get the document as a byte array or inputstream via OpenOffice's UNO api in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于 UNO API 的实现。我们能够用 PDF 做到这一点,
This depends on the implementation of the UNO API. We were able to do this with PDF,
对于 10 年后遇到这个问题的人,我必须包装输出流才能使其正常工作,
如果没有这个,我就会不断遇到
com.sun.star.lang.DisposeException
For anyone running into this 10 years later I had to wrap the output stream in order to get this to work
Without this I kept running into
com.sun.star.lang.DisposedException
我建议首先在本地保存文件(从 UNO API),然后在删除 [temp] 文件之前从 java 代码流式传输结果。这样做的原因是您可以将 OpenOffice 生成文档的问题与交付给客户端的问题分开。例如,如果您的文档无法生成,您可以生成错误,而不必担心部分写入的响应流式传输到客户端。另外,如果您还没有查看过工具,您可能需要查看Docmosis,它提供了冗余、性能优化和数据合并功能。它可以直接渲染到您提供的流(并且可能解决部分流结果问题)。
I would suggest saving the file locally first (from the UNO API) then streaming the result from your java code before deleting the [temp] file. The reason for this is that you can separate a problem with the generation of the document by OpenOffice from your delivery to the client. For example, if you're document fails to generate, you can produce an error without concerns for a partially written response streamed to a client. Also, if you haven't looked at tools already, you might want to look at Docmosis which provides redundancy, performance optimisation and data-merging functions. It can render directly to a stream that you supply (and presumably takes care of the partial-streamed-result issue).