Wednesday, April 19, 2006

Creating Word Reports Dynamically

From a while, I faced a problem in creating MS Word reports in web-based applications. Actually, I have an intranet application and I want to give the system user the ability to generate dynamic reports and view it in Microsoft Word.

After searching, I found that it's just a matter of changing the content type of the HTTP response. For example, if you are using ASP, then all what you will do is to make your report as a normal ASP page, but you will changing the content type of the response object to "application/msword". You can use any HTML tags as you need in side the report. But try to keep away from using images in your generated report as you may deal with URLs - and this depend on the connectivity state of the user.

Here is a sample code writen in ASP:


'-------------------------------------------------

'Creating MS Word Document Dynamically

'-------------------------------------------------

<%

Response.ContentType = "application/msword"

%>
<html>
<body>
<table>
<tr>
<%

' Your
Dynamic Content Here

%>
</tr>
</table>
</body>
</html>


That is all!. It's so simple which is not expected! Actually, you can use this method in generating Excel documents as well.

For more details about creating dynamic MS Word documents, read the following article:
http://www.infinetsoftware.com/content/officedocuments.asp

Also, for .NET guys, here is an article about generating MS Word reports in ASP.NET:
http://aspalliance.com/794

You may find some problems in displaying the report on the browser. For example, when you trying to call the URL of the code that generates the report, you may find a pop-up message asking if you want to save or open the document. To disable this dialoge from appearing, open the "Folder Opions" of your IE, then "File Types", find the "DOC" format, then click on "Advanced" Button. Then uncheck the "Confirm Open After Download". This will open the report directly after calling its scripting page. That is all!



0 comments: