Question

I am doing a project in struts1. There is a small problem.

I have an HTML report, I need to export this to a MS Word document and print it. How do I do this?

Was it helpful?

Solution

You can consider using Apache POI to output your report in Microsoft word. You can also refer to this link to see how is it done using Apache POI. A basic idea is that, in your class that extends Action, output the file using the HttpServletResponse. For example:

String filename = "words.doc";
p_response.setContentType("application/msword");
p_response.setHeader("Content-disposition",
"Attachment; filename=" + filename);

Good luck!

OTHER TIPS

Write out the report as plain and simple HTML. Then, lie to the browser:

response.setHeader("Content-Disposition", "attachment; filename=\""
            + reportName + ".doc\"");
response.setContentType("application/msword");

report.writeTo(response.getOutputStream()); // Actually writes HTML, not doc.

The browser will assume that it is a Word document and ask the user how they want to open it (i.e. with MS Word or OpenOffice). Both MS Word and OpenOffice are clever enough to not be fooled by the HTML contents of the .doc file, and will open it for the user to edit.

If the report must only be printed, I recommend exporting PDF instead, possibly using JasperReports, or just giving a plain HTML page to the user for printing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top