Export HTML to MS WORD & EXCEL in Java(JSP)

<%-- 
    Document   : Excel/Word Export
    Author     : Taher_JavaHunter
--%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Excel/Word Export</title>
    </head>
    <body>       
        <%
                    boolean isExcel = true;
                    if ("1".equals(request.getParameter("excel"))) {
                        isExcel = false;
                    }
                    boolean isWord = true;
                    if ("1".equals(request.getParameter("word"))) {
                        isWord = false;
                    }
        %>
        <%
                    if (!isExcel) {
                        String formName = "";
                        formName = "test.xls";
                        File filepath = new File(formName);
                        response.setContentType("application/vnd.ms-excel");
                        response.setHeader("Content-Disposition", "inline; filename=" + filepath);
                    }
                    if (!isWord) {
                        String formName = "";
                        formName = "test.doc";
                        File filepath = new File(formName);
                        response.setContentType("application/vnd.ms-word");
                        response.setHeader("Content-Disposition", "inline; filename=" + filepath);
                    }
        %>
        <table border="5" align="center">
            <tr>
                <th bgcolor="black" style="color: white;">Sr. No</th>
                <th bgcolor="black" style="color: white;">Name</th>
                <th bgcolor="black" style="color: white;">Address</th>
            </tr>
            <%
                        for (int i = 1; i <= 10; i++) {
            %>
            <tr>
                <td><%=i%></td>
                <td>Taher T</td>
                <td>Ahmedabad</td>
            </tr>
            <%}%>
        </table>
        <div align="center">
        <%if (isExcel || !isWord) {%>
        <a href="test.jsp?excel=1">Export to Excel</a>
        <%}%>
        <%if (isWord || !isExcel) {%>
        <a href="test.jsp?word=1">Export to Word</a>
        <%}%>
        </div>
    </body>
</html>

Advertisements
This entry was posted in JSP, Servlet. Bookmark the permalink.

13 Responses to Export HTML to MS WORD & EXCEL in Java(JSP)

  1. soumok says:

    dude..!! u r the man!!! really needed ths!!

  2. ^^ says:

    THX’S BRO . . . .MANY HELP WITH YOUR SCRIPT. .THX

  3. hari says:

    Nothing is happening when i click on the anchor links 😦
    Please assist me …Does a pop up window appears upon clicking on the anchor links ?

    • tahertinwala says:

      Yes a pop up does comes. May be problem with your browser setting please check it….

      • hari says:

        Dude Just by setting a content type header can we make a JSP page to emit a Word or Excel document. That’s not the case right?- we need to use a library like Apache POI which can create both those file types.

        Can you just tell me in which browser you tried this???and configuration changes in IE as well..

  4. kiran says:

    i’m getting error as test.jsp undefined

  5. R.C Bharath says:

    its asking test.jsp is not available…..

  6. Homen says:

    This method does not work when we use request.get Parameter() for take value from other page.why?Is there any method for such type of jsp file?

  7. rajalakshmi says:

    if i click export it send the code of the page i want output

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s