OpenOffice,将文档写入 servlet 响应

发布于 2024-09-06 19:23:11 字数 588 浏览 9 评论 0原文

目前,我们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无敌元气妹 2024-09-13 19:23:11

这取决于 UNO API 的实现。我们能够用 PDF 做到这一点,

    OutputStream os = response.getOutputStream();

    PropertyValue[] properties = new PropertyValue[2];
    PropertyValue property = new PropertyValue();
    property.Name = "FilterName";
    property.Value = FORMAT_WORD_97;
    properties[0] = property;
    PropertyValue streamProp = new PropertyValue();
    streamProp.Name = "OutputStream;
    streamProp.Value = os;
    properties[1] = streamProp;

    storable.storeAsURL("private:stream", properties);

This depends on the implementation of the UNO API. We were able to do this with PDF,

    OutputStream os = response.getOutputStream();

    PropertyValue[] properties = new PropertyValue[2];
    PropertyValue property = new PropertyValue();
    property.Name = "FilterName";
    property.Value = FORMAT_WORD_97;
    properties[0] = property;
    PropertyValue streamProp = new PropertyValue();
    streamProp.Name = "OutputStream;
    streamProp.Value = os;
    properties[1] = streamProp;

    storable.storeAsURL("private:stream", properties);
作死小能手 2024-09-13 19:23:11

对于 10 年后遇到这个问题的人,我必须包装输出流才能使其正常工作,

PropertyValue[] properties = new PropertyValue[2];
properties[0] = new PropertyValue();
properties[0].Name = "FilterName";
properties[0].Value = "writer_pdf_Export";
properties[1] = new PropertyValue();
properties[1].Name = "OutputStream";
properties[1].Value = new OutputStreamToXOutputStreamAdapter(outputStream);
storable.storeAsURL("private:stream", properties);

如果没有这个,我就会不断遇到 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

PropertyValue[] properties = new PropertyValue[2];
properties[0] = new PropertyValue();
properties[0].Name = "FilterName";
properties[0].Value = "writer_pdf_Export";
properties[1] = new PropertyValue();
properties[1].Name = "OutputStream";
properties[1].Value = new OutputStreamToXOutputStreamAdapter(outputStream);
storable.storeAsURL("private:stream", properties);

Without this I kept running into com.sun.star.lang.DisposedException

等风也等你 2024-09-13 19:23:11

我建议首先在本地保存文件(从 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文