Microsoft Knowledge Base Email Alertz

(301044) - This step-by-step article discusses how you can create a text-based document with Active Server Pages (ASP) and stream the document to the client using an Office Multipurpose Internet Mail Extensions (MIME) type. The text-based format demonstrated in...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 301044 - Last Review: January 17, 2007 - Revision: 5.1

How To Create an Office Document in an ASP Application

This article was previously published under Q301044

On This Page

SUMMARY

This step-by-step article discusses how you can create a text-based document with Active Server Pages (ASP) and stream the document to the client using an Office Multipurpose Internet Mail Extensions (MIME) type. The text-based format demonstrated in this article is HTML and the document is displayed by using the Excel and Word MIME types. There are several text-based formats that you can use to create Office documents using the same technique presented in this article.

You can create text-based documents for Excel by using the following formats:
  • Comma-separated (.csv)
  • Tab-delimited (.txt)
  • HTML
  • XML (Excel 2002)
Likewise, you can create text-based documents for Word by using the following formats:

  • Text
  • HTML
  • Rich Text Format (.rtf)

How to Create the Script

  1. Start any text or HTML editor and paste the following code into the editor:
    <%@ Language=VBScript %>
    <%
       Dim r, Amount, Tax
    
       'Change HTML header to specify Excel's MIME content type.
       Response.Buffer = TRUE
       Response.ContentType = "application/vnd.ms-excel"
    %>
    <HTML>
    <BODY>
      <TABLE>
        <TR>
          <TD><B>Order #</B></TD>
          <TD WIDTH="120" ALIGN="Right"><B>Amount</B></TD>
          <TD WIDTH="120" ALIGN="Right"><B>Tax</B></TD>
        </TR>
        <% For r= 1 to 20 %>
        <TR>
          <TD><%=r%></TD>
          <% Amount = FormatCurrency(Rnd()*1000,2) %>
          <TD><%=Amount%></TD>
          <% Tax = FormatCurrency(Amount*0.07,2) %>
          <TD><%=Tax%></TR>
        <% Next %>
        <TR>
          <TD>&#xa0;</TD>
        </TR>
        <TR>
          <TD>&#xa0;</TD><TD>=SUM(B2:B21)</TD><TD>=SUM(C2:C21)</TD>
        </TR>
      </TABLE>
    </BODY>
    </HTML>
    					
  2. Save the new script as Exceldoc.asp in the virtual root folder of your Web server. The default root is C:\Inetpub\Wwwroot.

How to Run the Script

  1. Start Microsoft Internet Explorer.
  2. In the address bar, type http://YourWebServer/ExcelDoc.asp (where YourWebServer is the name of your Web server) and press ENTER.
  3. After the table is opened in Excel, examine the results. Note the formatting and the formulas in cells B23 and C23.

How to Try It Again

Next, modify the script so that the the document opens in Word instead of Excel:

  1. In the script, modify the MIME type as follows:
       Response.ContentType = "application/msword"
       
    					
  2. Save the modified document in the virtual root folder of your Web server as Worddoc.asp.
  3. Start Internet Explorer and browse to http://YourWebServer/WordDoc.asp (where YourWebServer is the name of your Web server).

Toubleshooting

Using a text-based format for your Office documents can provide a solution on your Web server that is very scalable and provides good performance. A disadvantage of using text-based formats is that you are limited in the Office features that you can use. Some developers may choose to use Automation for Office document creation; although Automation in a client-side environment gives you the greatest flexibility in exploiting all the features that are available in Office, it can generate some problems for a server-side application. If you find that using a text-based document format does not deliver the level of document creation you need, you may want to see the following Knowledge Base article to determine if Automation might be a solution that is right for you:

257757  (http://kbalertz.com/Feedback.aspx?kbNumber=257757/EN-US/ ) INFO: Considerations for Server-Side Automation of Office

REFERENCES

For more information, see the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)

APPLIES TO
  • Microsoft Office 2000 Developer Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.01
  • Microsoft Internet Explorer 5.5
  • Microsoft Active Server Pages 4.0
Keywords: 
kbhowto kbhowtomaster KB301044
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate