OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Send a Calc document to the web user

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
DelphaX
General User
General User


Joined: 13 Oct 2004
Posts: 6

PostPosted: Wed Nov 17, 2004 3:17 am    Post subject: Send a Calc document to the web user Reply with quote

Hi,

I'm currently developping a Struts web application (thus Java UNO automation) which needs to export database data to SXC and send it to the user. I already know how to build the Calc document and fill it with data, but what I don't know is how to send it to the user (i.e get a Stream from the XSpreadSheetDocument that I would send to response.getOutputStream(). I would also need a correct MIME type for the stream). I know I could store the file on the server via an XStorable's storeToUrl() method, but I can't do it as I'm not allowed to dynamically create any file on the server, even a temporary one.

Could anyone help, please ?

Thanks in advance...
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Nov 17, 2004 7:43 am    Post subject: Reply with quote

Here is my best guess....

Create a Java class that implement UNO's interface XOutputStream.

It may also be necessary to implement XSeekable, I'm not sure. See links below.

When you call storeToURL, you can pass this url... "private:stream". As the second argument to storeToURL, you must pass an array of PropertyValue. One of the property values must be....
Name = "OutputStream"
Value = ...your java object implementing XOutputStream....

Here is an example in Basic that calls storeToURL, but passing the URL as "private:stream", and an array of property values that has OutputStream equal to an XOutputStream. The XOutputStream in this Basic example is obtained by using SimpleFileAccess to get an XOutputStream to a local disk file.

I put this code into a spreadsheet, and indeed the document saves. So it is definitely possible to use the OutputStream property combined with a "priavte:stream" url when calling storeToURL.

Code:
Sub Main
   ' The URL we wish to save to.
   cSaveToFile = "C:\Documents and Settings\dbrewer\Desktop\MeowMix.sxc"
   cSaveToUrl = ConvertToURL( cSaveToFile )
   ' The string variable cSaveToUrl now contains a URL.
   
   ' Get an XOutputStream which can write to this file.
   oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
   oOutputStream = oSimpleFileAccess.openFileWrite( cSaveToUrl )
   ' The variable oOutputStream now contains an XOutputStream
   '  that can write to a local disk file.
   
   ' Now save THIS document to the XOutpuStream
   ThisComponent.storeToURL( "private:stream",_
      Array(_
         MakePropertyValue( "OutputStream", oOutputStream ) ) )
End Sub

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function


Even though you are in Java, if you study this Basic example, you should see what I am doing. Only look at the line that says "storeToURL". The lines above it are just obtaining an XOutputStream from a local disk file.

The storeToURL will call methods of your XOutputStream to write out the data. Your class that implements XOutputStream needs to in turn push this data out to the http response stream.

If it turns out to be necessary to implement XSeekable, then you may not be any better off than simply saving the document to a local file, and then reading that file, pushing it out to the web browser, and then deleting the temp file. Well, there is another possibility. If it turns out that you must implement XSeekable, then you could provide an XOutputStream/XSeekable object to storeToURL, and save the data stream into memory, as a large StringBuffer. Then when OOo is finished saving it, push those bytes down to the http response stream.

I hope that explanation made sense.

Here are some related past discussions.....

Load a document from an XInputStream using private:stream
Load a document from database blob
http://www.oooforum.org/forum/viewtopic.php?t=6807
http://www.oooforum.org/forum/viewtopic.php?t=7032
http://www.oooforum.org/forum/viewtopic.php?t=7010
store to blob, technique but no working example
http://www.oooforum.org/forum/viewtopic.php?t=7011
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
sripathyramesh
General User
General User


Joined: 11 Nov 2005
Posts: 14

PostPosted: Mon Jan 02, 2006 5:22 am    Post subject: Reply with quote

Hi,

Related to this discussion, I use an output stream wrapper as in http://www.oooforum.org/forum/viewtopic.phtml?p=72906#72906 to write a calc document to the output stream.

I set the mime type to "application/vnd.ms-excel"
I set the FilterName as "MS Excel 97"
I set the header of the response as response.setHeader("Content-Disposition", "attachment;filename=test.xls");
I use the xStore.storeToURL("private:stream",lProperties) function to write to the output stream

But the resulting excel stream is often junk when opened in Excel. But interestingly, the same code works on some machines while on others it just dumps some data which the Excel / OpenOffice doesn't recogonize.

Any ideas on how to solve this?

Regards,
Ramesh
Back to top
View user's profile Send private message
n0mer
General User
General User


Joined: 20 Mar 2006
Posts: 46

PostPosted: Mon Jul 03, 2006 3:50 am    Post subject: Reply with quote

sripathyramesh wrote:
But the resulting excel stream is often junk when opened in Excel. But interestingly, the same code works on some machines while on others it just dumps some data which the Excel / OpenOffice doesn't recogonize.

This code works fine:
Code:

/*
    public ActionForward executeInternal(ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response)
*/
            response.setContentType(contentType);
            response.setHeader(
                "Content-Disposition",
                "inline; filename=\"" + reportFileName + "\""
            );
            response.setHeader("Cache-Control", "no-cache"); //?
            response.setIntHeader("Content-Length", byteArrayOutputStream.size());

            //BODY
            byteArrayOutputStream.writeTo(response.getOutputStream());

            byteArrayOutputStream.reset();
            response.getOutputStream().flush();
            response.getOutputStream().close();

Back to top
View user's profile Send private message Visit poster's website
sripathyramesh
General User
General User


Joined: 11 Nov 2005
Posts: 14

PostPosted: Mon Jul 03, 2006 11:58 pm    Post subject: Reply with quote

Thanks n0mer.

Well I should have replied to this post and closed this thread. It was actually a very silly error of mine which I already corrected. Instead of writing to the OutputStream, I was writing to the response.getWriter().print(new String(the byte[])); Hence the issue.

It was a very silly error which I fixed and it works fine now. But even with the old code, it used to work on certain machines while did not work on certain other machines. Don't know why.

Regards,
Ramesh
Back to top
View user's profile Send private message
n0mer
General User
General User


Joined: 20 Mar 2006
Posts: 46

PostPosted: Tue Jul 04, 2006 12:27 am    Post subject: Reply with quote

sripathyramesh wrote:
Instead of writing to the OutputStream, I was writing to the response.getWriter().print(new String(the byte[])); Hence the issue.

http://tomcat.apache.org/tomcat-5.0-doc/servletapi/javax/servlet/ServletResponse.html
Quote:
To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group