views:

84

answers:

4

I am helping someone out with a javascript-based web app (even though I know next to nothing about web development) and we are unsure about the best way to implement a feature we'd like to have.

Basically, the user will be using our tool to view all kinds of boring data in tables, columns, etc. via javascript. We want to implement a feature where the user can click a button or link that then allows the user to download the displayed data in a .doc file.

Our basic idea so far is something like:

  • call a Java function on the server with the desired data passed in as a String when the link is clicked
  • generate the .doc file on the server
  • automatically "open" a link to the file in the client's browser to initiate the download

Is this possible? If so, is it feasible? Or, can you recommend a better solution?

edit: the data does not reside on the server; rather, it is queried from a SQL database

+2  A: 

Yep, its possible. Your saviour is the Apache POI library. Its HWPF library will help you generate Microsoft word files using java. The rest is just clever use of HTTP.

Here Be Wolves
+1  A: 

Your basic idea sounds a bit Rube-Goldbergesque.

Is the data you want in the document present on the server? If so, then all you need to do is display a plain HTML link with GET parameters that describes the data (i.e. data for customer X from date A to date B). The link will be handled on the server by a Servlet that gets the data and produces the .DOC file as its output to be downloaded by the browser - a very simple one-step process that doesn't even involve any JavaScript.

Michael Borgwardt
no, the data is being queried from a SQL database
ignorantslut
Which means the server can simply run the same query again - it has done it before, it's not the javaScript that directly accesses the DB, is it?
Michael Borgwardt
A: 

Passing large amount data as GET/POST around might not be the best idea. You could just pass in the same parameters you used to generate the HTML page earlier. You don't even need to use 3rd party library to generate DOC. You could just generate a plain old HTML file with DOC extension and Word will be happy to open it.

kd304
thats a nice trick :) but the file format won't really be DOC now, will it :)
Here Be Wolves
that's a good idea, but it would be nice to keep the table formatting
ignorantslut
True, but if the user sees the results opening in Word, nobody cares (from experience). We did the same trick for export only excel spreadsheets - no problem with comma-semicolon separator unlike in csv.
kd304
A: 

Sounds like Docmosis Java library could help - check out theonline demo since shows it something similar to what you're asking - generating a real doc file from a web site based on selections in the web page. Docmosis can query from databases and run pretty much anywhere.

jowierun