FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

ASP Tutorial: Dynamically generating MS Word/MS Excel Document using ASP

jawahar
11/8/2005 1:10:44 AM, Views: 3886
The following tutorial with code can be used to generate MS Word/MS-Excel documents dynamically from your site :

   ASP has the ability to dynamically output virtually any type of file, provided that the correct file format is being used. The easiest type to write would obviously be text, which is what we are already doing when we output webpages. The other type, binary, is harder to do since ASP doesn't natively support binary variables. Today we will be using standard text output and the appropriate MIME file type to turn out a dynamic Microsoft Word document and a Microsoft Excel document.

ContentType

   The first thing we need to do is set the appropriate file type so that the browser knows what to do with the file. We do this using the ContentType setting of the Response object. For Word, the content type is application/msword. For Excel, its application/vnd.ms-excel. So we would use the either of the lines of code below for Word or Excel documents, respectively. This line must come before ANY data is output.

Response.ContentType = "application/msword"

Response.ContentType = "application/vnd.ms-excel"

   If the user is using Internet Explorer, the file will most likely be sent to an Office plugin that will show it within the browser window. For different reasons, this can sometimes be annoying for both the user and the programmer. If you do not want your file to be displayed in the user's browser window, and instead want to force the save dialog box to pop up, use the line below right before/after setting the ContentType.

Response.AddHeader "Content-Disposition", "attachment;filename=NAME.doc"

Writing the File

   Since Word and Excel will take text input, we will be outputting our data in the same way we do with HTML. Actually, the format we will be using is HTML! Even color formatting, CSS, and meta tags are supported.

   For Word, you can just write HTML as you normally would, except you are gearing it towards Word viewing. HTML tables, CSS, and meta tags are all supported. You can use the meta tags to set the document properties that Word has, such as Title and Author. Being in Word, you probably would want to stay away from images unless you know the user will always be connected to the internet to actually see them.

Sample Word Document

<%
Response.ContentType = "application/msword"
Response.AddHeader "Content-Disposition",
"attachment;filename=receipt.doc"

Dim strName, strAddress

strName = Request.Form("Name")
strAddress = Request.Form("Address")

%>
<html>

<head>
<title>Word Test</title>
<meta name="Author" content="Acme Toys">

<style type="text/css">

.BigTitle {
font-family: Verdana, Sans-Serif;
font-size: 20pt;
font-weight: bold;
color: #004080;
}

.UserDetails {
font-family: Courier New, Monospace;
font-size: 12pt;
}

</style>

</head>

<body>

<span class="BigTitle">Sample Receipt</span>

<p>Thank you for purchasing, your order will be shipped within 2 business days. Below are the shipping details, provided by you.</p>

<p>
<table cellspacing="1" cellpadding="1" border="0">
<tr>
<td><b>Name:</b></td>
<td><span class="UserDetails"><%=strName%></span></td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><span class="UserDetails"><%=strAddress%></span></td>
</tr>
</table>
</p>

</body>

</html>

   The same applies for Excel as does for Word except you are writing an HTML table to make your sheet. You can even use Excels functions by outputting the function as the table cell value.

Sample Excel Document

<%
Response.AddHeader "Content-Disposition",
"attachment;filename=invoice.xls"
Response.ContentType = "application/vnd.ms-excel"

Dim strName, intOrderCount, intPricePer

strName = Request.Form("Name")
intOrderCount = Request.Form("Orders")
intPricePer = Request.Form("Price")

%>
<table width="90%" border="2" bordercolor="green">
<tr>
<th width="70%"><b>Name</b></th>
<th width="15%"><b>Price</b></th>
<th width="15%"><b>Quantity</b></th>
</tr>
<tr>
<td width="70%"><%=strName%></td>
<td width="15%"><%=intPricePer%></td>
<td width="15%"><%=intOrderCount%></td>
</tr>
<tr>
<td colspan="2" align="right"><b>Total:</b></td>
<td align="left">=product(b2,c2)</td>
</tr>
</table>
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Avg Visitor Rating: Average Visitor Rating is 4 out of 5
Number of Ratings : 5 Votes
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
ASP RSS Feed
Most Popular Tutorial
Most Popular Solution
Top Rated
Top Rankers
Overall
1. jawahar (2900)
Yearly -2008
1. jawahar (100)
Expertsforge Sponsors
bnrtop