<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>infinetSoftware &#187; Archive</title>
	<atom:link href="http://www.infinetsoftware.com/blog/category/archive/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.infinetsoftware.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 03 May 2010 16:04:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Write Office Documents with ASP</title>
		<link>http://www.infinetsoftware.com/blog/15-write-office-docs-with-asp/</link>
		<comments>http://www.infinetsoftware.com/blog/15-write-office-docs-with-asp/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 20:46:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Archive]]></category>

		<guid isPermaLink="false">http://www.infinetsoftware.com/blog/?p=15</guid>
		<description><![CDATA[
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&#8217;t natively
support binary variables. Today we will [...]]]></description>
			<content:encoded><![CDATA[<p>
ASP has the ability to dynamically output virtually any type of file,<br />
provided that the correct file format is being used. The easiest type to write<br />
would obviously be text, which is what we are already doing when we output<br />
webpages. The other type, binary, is harder to do since ASP doesn&#8217;t natively<br />
support binary variables. Today we will be using standard text output and the<br />
appropriate MIME file type to turn out a dynamic Microsoft Word document and a<br />
Microsoft Excel document.
</p>
<p><b>ContentType</b></p>
<p>
The first thing we need to do is set the appropriate file type so that the<br />
browser knows what to do with the file. We do this using the ContentType setting<br />
of the Response object. For Word, the content type is <code>application/msword</code>. For<br />
Excel, its <code>application/vnd.ms-excel</code>. So we would use the either of the lines<br />
of code below for Word or Excel documents, respectively. This line must come before ANY<br />
data is output.
</p>
<table width="600" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="#E7E7E7">
<td><code>Response.ContentType = "application/msword"</code></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr bgcolor="#E7E7E7">
<td><code>Response.ContentType = "application/vnd.ms-excel"</code></td>
</tr>
</table>
<p>
If the user is using Internet Explorer, the file will most likely be sent to<br />
an Office plugin that will show it within the browser window. For different<br />
reasons, this can sometimes be annoying for both the user and the programmer. If<br />
you do not want your file to be displayed in the user&#8217;s browser window, and<br />
instead want to force the save dialog box to pop up, use the line below right<br />
before/after setting the ContentType.
</p>
<table width="600" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="#E7E7E7">
<td><code>Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment;filename=NAME.doc&quot;</code></td>
</tr>
</table>
<p><b>Writing the File</b></p>
<p>
Since Word and Excel will take text input, we will be outputting our data in the<br />
same way we do with HTML. Actually, the format we will be using is HTML! Even<br />
color formatting, CSS, and meta tags are supported.</p>
<p>
For Word, you can just write HTML as you normally would, except you are gearing<br />
it towards Word viewing. HTML tables, CSS, and meta tags are all supported. You<br />
can use the meta tags to set the document properties that Word has, such as Title<br />
and Author. Being in Word, you probably would want to stay away from images<br />
unless you know the user will always be connected to the internet to actually see them.
</p>
<table width="600" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><b>Sample Word Document</b></td>
</tr>
<tr bgcolor="#E7E7E7">
<td><code>&lt;%<br />
    Response.ContentType = &quot;application/msword&quot;<br />
    Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment;filename=receipt.doc&quot;</p>
<p>    Dim strName, strAddress</p>
<p>    strName = Request.Form(&quot;Name&quot;)<br />
    strAddress = Request.Form(&quot;Address&quot;)</p>
<p>    %&gt;<br />
    &lt;html&gt;</p>
<p>    &lt;head&gt;<br />
    &lt;title&gt;Word Test&lt;/title&gt;<br />
    &lt;meta name=&quot;Author&quot; content=&quot;Acme Toys&quot;&gt;</p>
<p>    &lt;style type=&quot;text/css&quot;&gt;</p>
<p>    .BigTitle {<br />
    font-family: Verdana, Sans-Serif;<br />
    font-size: 20pt;<br />
    font-weight: bold;<br />
    color: #004080;<br />
    }</p>
<p>    .UserDetails {<br />
    font-family: Courier New, Monospace;<br />
    font-size: 12pt;<br />
    }</p>
<p>    &lt;/style&gt;</p>
<p>    &lt;/head&gt;</p>
<p>    &lt;body&gt;</p>
<p>    &lt;span class=&quot;BigTitle&quot;&gt;Sample Receipt&lt;/span&gt;</p>
<p>    &lt;p&gt;<br />
    Thank you for purchasing, your order will be shipped within 2 business<br />
    days. Below are the shipping details, provided by you.<br />
    &lt;/p&gt;</p>
<p>    &lt;p&gt;<br />
    &lt;table cellspacing=&quot;1&quot; cellpadding=&quot;1&quot; border=&quot;0&quot;&gt;<br />
    &lt;tr&gt;<br />
    &lt;td&gt;&lt;b&gt;Name:&lt;/b&gt;&lt;/td&gt;<br />
    &lt;td&gt;&lt;span class=&quot;UserDetails&quot;&gt;&lt;%=strName%&gt;&lt;/span&gt;&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
    &lt;tr&gt;<br />
    &lt;td&gt;&lt;b&gt;Address:&lt;/b&gt;&lt;/td&gt;<br />
    &lt;td&gt;&lt;span class=&quot;UserDetails&quot;&gt;&lt;%=strAddress%&gt;&lt;/span&gt;&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
    &lt;/table&gt;<br />
    &lt;/p&gt;</p>
<p>    &lt;/body&gt;</p>
<p>    &lt;/html&gt;</code></td>
</tr>
</table>
<p>
The same applies for Excel as does for Word except you are writing an HTML<br />
table to make your sheet. You can even use Excels functions by outputting the<br />
function as the table cell value.</p>
<table width="600" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><b>Sample Excel Document</b></td>
</tr>
<tr bgcolor="#E7E7E7">
<td><code>&lt;%<br />
    Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment;filename=invoice.xls&quot;<br />
    Response.ContentType = &quot;application/vnd.ms-excel&quot;</p>
<p>    Dim strName, intOrderCount, intPricePer</p>
<p>    strName = Request.Form(&quot;Name&quot;)<br />
    intOrderCount = Request.Form(&quot;Orders&quot;)<br />
    intPricePer = Request.Form(&quot;Price&quot;)</p>
<p>    %&gt;<br />
    &lt;table width=&quot;90%&quot; border=&quot;2&quot; bordercolor=&quot;green&quot;&gt;<br />
    &lt;tr&gt;<br />
    &lt;th width=&quot;70%&quot;&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/th&gt;<br />
    &lt;th width=&quot;15%&quot;&gt;&lt;b&gt;Price&lt;/b&gt;&lt;/th&gt;<br />
    &lt;th width=&quot;15%&quot;&gt;&lt;b&gt;Quantity&lt;/b&gt;&lt;/th&gt;<br />
    &lt;/tr&gt;<br />
    &lt;tr&gt;<br />
    &lt;td width=&quot;70%&quot;&gt;&lt;%=strName%&gt;&lt;/td&gt;<br />
    &lt;td width=&quot;15%&quot;&gt;&lt;%=intPricePer%&gt;&lt;/td&gt;<br />
    &lt;td width=&quot;15%&quot;&gt;&lt;%=intOrderCount%&gt;&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
    &lt;tr&gt;<br />
    &lt;td colspan=&quot;2&quot; align=&quot;right&quot;&gt;&lt;b&gt;Total:&lt;/b&gt;&lt;/td&gt;<br />
    &lt;td align=&quot;left&quot;&gt;=product(b2,c2)&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
    &lt;/table&gt;</code></td>
</tr>
</table>
<p>
As you can see, with the simplicity of writing Word/Excel documents, you can<br />
easily implement them into your site where needed. Other possible uses<br />
include expense sheets retrieved from a database and formatted in<br />
Excel, or printable forms in Word with dynamic user information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.infinetsoftware.com/blog/15-write-office-docs-with-asp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
