How to Export Web Page to Word (in JSP)

In previous post we saw how we can export the content displayed on a web page to an excel file. In this tutorial we will see how we can export web page to word file. Although chances of your end users demanding an Export to Word functionality is less than Export to Excel, but it is still handy to know how this can be handled if need be.

Lets assume this is our existing page where we want to add Export to Word functionality:

Export web page to word

Here is the existing source code of this web page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Export to Word - Demo</title>
</head>
<body>
	This is the plain text. 
	<p>
	<i>This is the italic text. </i>
	<p>
	<b>This is the bold text. </b>
	<p>
	<s>This is the strike text.</s> 
	<p>
	<font color="green">This is the color text. </font>
	<p>
	<a href="#">This is hyperlink. </a>
	<p>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</body>
</html>

We will add a link “Export to Word” which will export the contents of the page to a word file. The page will look something like this:

Export web page to word

Here is the new version code of the jsp we showed above. This version adds “Export to Word” link and its corresponding functionality:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Export to Word - Demo</title>
</head>
<body>
	<%
		String exportToWord = request.getParameter("exportToWord");
		if (exportToWord != null
				&& exportToWord.toString().equalsIgnoreCase("YES")) {
			response.setContentType("application/vnd.ms-word");
			response.setHeader("Content-Disposition", "inline; filename="
					+ "word.doc");

		}
	%>
	This is the plain text. 
	<p>
	<i>This is the italic text. </i>
	<p>
	<b>This is the bold text. </b>
	<p>
	<s>This is the strike text.</s> 
	<p>
	<font color="green">This is the color text. </font>
	<p>
	<a href="#">This is hyperlink. </a>
	<p>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<%
		if (exportToWord == null) {
	%>
	<a href="word.jsp?exportToWord=YES">Export to Word</a>
	<%
		}
	%>
</body>
</html>

Export web page to word code explanation:

1) In this version of JSP, when you click on the “Export to Word” link, the request is sent to the same page (word.jsp) but with a URL parameter exportToWord=YES.

Around The World Flights - Promo Code WORLD15
	<a href="word.jsp?exportToWord=YES">Export to Word</a>

2) The JSP checks for this parameter right at the beginning. If this parameter is found with value YES, we set the content type of the response to word and specify the filename of the word file which will open on the user’s computer.

		String exportToWord = request.getParameter("exportToWord");
		if (exportToWord != null
				&& exportToWord.toString().equalsIgnoreCase("YES")) {
			response.setContentType("application/vnd.ms-word");
			response.setHeader("Content-Disposition", "inline; filename="
					+ "word.doc");

		}

3) All the contents on the page are exported when you click on the “Export to Word”. However we might not want the “Export to Word” link itself to appear in the exported word file. To prevent this from appearing, we add an if condition to check whether exportToWord parameter is present or not. If it is, then it means that the contents are to be exported to word so we don’t include that link. If the parameter is not present, then it means we are just loading the JSP page on browser, so the link is printed on page.

	<%
		if (exportToWord == null) {
	%>
	<a href="word.jsp?exportToWord=YES">Export to Word</a>
	<%
		}
	%>

Export web page to word output:

When you click the link, you should get a popup asking you to Open or Save the file. Click open, you should see the file as below:

Export web page to word

As you can see, the Export to Word functionality we just added also preserves the formatting of the data we export.

You can download the eclipse project for the code explained in this tutorial:

JavaWordExport.zip (5KB)

Please consider following our blog (via Email, Twitter, Facebook and/or Google+ – See the right sidebar) for more interesting posts.

31 responses to “How to Export Web Page to Word (in JSP)”

  1. Export to Word

    [...] where in they provide an example of adding Export to Word functionality to an existing JSP: How to Export Web Page to Word (in JSP) – QuicklyJava Reply With Quote « does any one know how to run javaanpr | – [...]

  2. lista de email

    it was nice to read your blog. the thoughts are very well laid out and it was refreshing to read. i was able to find the information that i was looking for. thanks.

    1. Newengland

      This article is a home run, pure and simple!

  3. lista de email

    every time i want to learn something good, i access your website, because of the great structure and coherent ideas please keep providing such good information.

    1. Zaiyah

      Yeah that’s what I’m talking about baby–nice work!

  4. Samiksha

    Its really excellent idea. Thank you very much for posting this.

  5. tdvofr

    Wow, incredible blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is wonderful, as well as the content!. Thanks For Your article about How to Export Web Page to Word (in JSP) – QuicklyJava .

    1. Jumah

      Hey, that’s a clever way of thinking about it.

  6. Leonel Vasser

    Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is wonderful, as well as the content!. Thanks For Your article about How to Export Web Page to Word (in JSP) – QuicklyJava .

    1. Bekzat

      That’s the smart thinking we could all benefit from.

  7. stocking pantyhose

    This post is the greatest. You have a new fan! I can’t wait for the next update, favorite!

  8. Hop Over To These Guys

    Your style is unique in comparison to other folks I’ve read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this page.

  9. Quentin Frink

    I the efforts you have put in this, appreciate it for all the great content .

  10. Fernanda Binnicker

    I truly love your site.. Pleasant colors & theme. Did you develop this amazing site yourself? Please reply back as I’m wanting to create my own website and would love to know where you got this from or exactly what the theme is named. Cheers!

  11. free lingerie movies

    Too many times I passed over this site, and that was a mistake. I’m glad I will be back!

  12. Mantenimiento

    I literally jumped out of my chair and danced after reading this!

  13. baju bayi

    Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is excellent, let alone the content!. Thanks For Your article about How to Export Web Page to Word (in JSP) – QuicklyJava .

  14. Walker Smithee

    Wow! This could be one particular of the most beneficial blogs We’ve ever arrive across on this subject. Basically Fantastic. I’m also a specialist in this topic therefore I can understand your hard work.

  15. shankar

    Thanks Buddy..!

    nice to share this script.good one and working.

    quicklyjava.com rocks….!

  16. setum2013.com

    good quickly java is Quickly indeed………..

    its just open with MSword

  17. suresh

    thank you.. it helps me lot… can u please tell me how to export data from a jsp page to PDF format.. with simple example.

  18. middlechildreviews.com

    I got this website from my pal who shared with me about this website and at the moment this time
    I am browsing this web page and reading very informative articles or reviews here.

  19. Clarke

    Thanks for your marvelous posting! I really enjoyed reading it, you could be a
    great author. I will ensure that I bookmark your blog and definitely will come back at some point.
    I want to encourage one to continue your great job, have a nice
    afternoon!

  20. teja

    I have a problem..when i retrive values from database and use those values in the fields of my html page and then export to word..I get null values in the word in the palce of database retrived values…Plzz help

  21. homen

    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?

  22. sundari

    hey hi i want jsp page ouput should be converted to word document with headers and footers any one know this please help me out

  23. anusha

    can anybody know how to add headers in addition to exporting jsp content to a word document.

  24. vikram

    String exportToWord = request.getParameter(“exportToWord”);
    String staffno=request.getParameter(“staffno”);
    String case_id=request.getParameter(“case_id”);

    include the request.getparameter inside the export to word code as above

Leave a Reply