Search code examples
javajspstruts1

Export an HTML report into MS Word using Java code


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?


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!