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:
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> </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:
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>
<%
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.
<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:
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:
Please consider following our blog (via Email, Twitter, Facebook and/or Google+ – See the right sidebar) for more interesting posts.






[...] 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 | – [...]
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.
Thanks! May I suggest you to follow our blog (via Email, Twitter, Facebook and/or Google+) for more interesting posts!
That’s the smart thinking we could all benefit from.
This article is a home run, pure and simple!
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.
Yeah that’s what I’m talking about baby–nice work!
Its really excellent idea. Thank you very much for posting this.
Thanks Samiksha for stopping by. Please follow our blog on Facebook, Twitter or Google+. You can also sign up to get our latest posts directly in your mailbox.
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 .
Hey, that’s a clever way of thinking about it.
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 .
That’s the smart thinking we could all benefit from.
This post is the greatest. You have a new fan! I can’t wait for the next update, favorite!
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.
I the efforts you have put in this, appreciate it for all the great content .
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!
Too many times I passed over this site, and that was a mistake. I’m glad I will be back!
I literally jumped out of my chair and danced after reading this!
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 .
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.
Thanks Buddy..!
nice to share this script.good one and working.
quicklyjava.com rocks….!
good quickly java is Quickly indeed………..
its just open with MSword
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.