1

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
  • What type of web server are you using?
    – akarnokd
    Jun 17, 2009 at 11:42
  • a java enabled one, i presume
    – jrharshath
    Jun 17, 2009 at 11:50

4 Answers 4

3

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.

1

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.

1
  • 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? Jun 17, 2009 at 12:02
0

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.

3
  • thats a nice trick :) but the file format won't really be DOC now, will it :)
    – jrharshath
    Jun 17, 2009 at 11:49
  • that's a good idea, but it would be nice to keep the table formatting Jun 17, 2009 at 11:54
  • 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.
    – akarnokd
    Jun 17, 2009 at 12:01
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.