c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


Creating Excel WorkSheets with ASP



By Wayne Berry
In this article we will discuss and demonstrate how to create Microsoft Excel worksheets from Active Server pages. Though there are many ways that this can be done, we have chosen a method for the example that conserves server load and puts the majority work on the client.
Three Techniques
The other methods however are worth mentioning, since there are many ways to come to the same ends. The ends in this case is having real time data served from the web server and available in Excel upon the clients machine. The three methods are as follows: 1) Creating the VBA component on the web server and either streaming it down to the client or linking to the finished component from a separate page. 2) Making Excel call the web server for the data using HTTP and inserting it into the right areas of the Excel WorkSheet. 3) Creating an HTML stream that Excel can interrupt and translate into a Excel WorkSheet. The third technique is the one that I am going to discuss in this article, but first let take a brief look at the other options.
The First Technique
The first technique is to create a VBA component, in this case an Excel WorkSheet, on the web server and either streaming it down to the browser or linking to it from another page. This technique at first glance seems the most appealing. Since you can create COM object on an ASP page by calling Server.CreateObject with the name of the object that you want to create. Then with multiple calls to method and properties of the object you could fill in the data of the WorkSheet. However, at second glance, this is not the best technique. To start with it would cause considerable server load if the server was receiving multiple requests for these objects. Another problem arises when opening the object. Each object must have a unique file name, objects with shared file names give all by the first instantiation read only permission. This means that for every request to the web server you must generate a unique name for that object. Since you are generating all these files you must also clean up the files, or they will start to consume disk space. The clean up mechanism will have to coordinate with the request, so files are not deleted when they are being used. You can see how the problems start to mount. Another issues is the size of the Excel files that you will be streaming, Excel files for small WorkSheets are usually big in comparison to an HTML page or a graphic. The large size cause network strain and slow response time for sites with minimal bandwidth. The final problem is, VBA objects are not multi-threaded and were never intended to be used as a server. Imagine this, you are a VBA object and you happily live on a machine where your owner sometime opens you up to view a document. This is what your designers intended and this is how you where built. You are very proud that you take up a minimum amount of memory and can run on a 486 machine without problems. Suddenly one day your owner throws you on a server and starts to make you open several requests a second. Even with addition memory and a fast processor you where never tested with multiple open instances and you fail miserable at performing your job. Out of the three techniques this is the most interest, but the least workable.
The Second Technique
The second technique is to have Excel call the web server and use the data to fill in WorkSheet. This technique is actually fairly easy to do in Excel 97. Microsoft has added additional functionality to Excel 97 called embedded HTML. There is also a wizard to give you a hand. The typical user experience would be to open an Excel document, push a button that is embedded in a cell, the web site is called and the data appears in the Excel WorkSheet. Through this method is very useable in my opinion it is not as powerful as the third method.
The Third Technique
The third technique is to create an HTML stream that Excel can interrupt and translate into an Excel WorkSheet. We will spend the remainder of the article describing how this works and how to use it. The only major flaw in this technique is that we can only fill in one Excel WorkSheet at a time, multiple Excel Worksheet in a WorkBook will not work. Before we get started there are a couple of things we need to discuss about Microsoft Internet Explorer and Excel 97.
Internet Explorer 3.0
Internet Explorer 3.0 is capable of displaying an Microsoft Excel Worksheet just as it would be displayed in the Excel application, if Excel is installed. The usual way of displaying the worksheet in the IE browser is to enter the URL of an excel workbook with the extension xls. IE then uses the extension to figure out the mime type and the application the mime type and the extension are associated with. IE needs to determine the mime type from the extension since IIS and other web servers pass the data stream back as a application/octect-stream. IE changes the mime type to application/vnd.ms-excel. If the IIS server returned application/vnd.ms-excel as the mime type, or Content-Type as defined in HTTP, IE would try to interrupt the data stream as an Excel WorkSheet. By interrupt I mean launch Microsoft Excel and display the data within the IE using Excel. So, any application sending back a Content-Type of application/vnd.ms-excel will cause IE to display the data as an Excel WorkSheet.
Excel 97
In theory we know how to make Excel come up in the IE Browser, now all we have to do is pass Excel the correct data stream. This might be the trickiest part and also the easiest with Excel 97. The example that we are about to present will not work with earlier versions of Excel, the examples will only work with Excel 97. Excel 97 contains a special piece of code that will generate an Excel WorkSheet from an HTML table. What this means is that we can pass an HTML table back as the data stream and Excel will interpret the data stream as a WorkSheet. Much easier then generating a data stream that looks like an Excel WorkSheet.
Summary
With Excel 97 and Internet Explorer 3.0 we can create an Excel spreadsheet from an HTML table by changing the response Content-Type to application/vnd.ms-excel and sending back an HTML table as the data stream. Why would we want to do this?
Real Time Data, Anytime
If you create your HTML tables using Active Server pages to dynamically fill in the data, you can produce reports using ADO that reflect the current data in the database. As an example, analysis of page hits. Most Internet companies have page hit reports that are generated and viewable through HTML browsers. By changing the mime type on these reports you can display them as Excel WorkSheets that can later be graphed or manipulated.
Reduced Server Load
If you have a dynamically generated web page that reports page hits, it probably has a total at the bottom. The lazy way to generate a total is to execute another SQL call after the original call to display all the hits. Something like:
SELECT 
SUM(Page_Hits) FROM
PageTable WHERE
DATEDIFF(day,Page_Date,GetDate()) <  1 
I call this the lazy way since it makes another call to the database (SQL Server in this case) and causes extra the SQL Sever load. Especially since the aggregate does a table lock, and the GetDate() and DATEDIFF are expensive.

A second technique is to add the page hits up from the original call that displays all the hits. You can easily do this in an Active Server Page with a little code like this:

<%
Total=0
Do While Not RS.eof  
%>
<TR>
&l%TD>
<%=RS("Page_Hits)%>
</TD>
</TR>
<%
Total = Total + RS("Page_Hits)
RS.MoveNext
Loop
%>
<TR>
<TD>
<B><%=Total%></B>
</TD>
</TR>
This technique however has some problems when it comes to more complicated aggregates then SUM. For instance, what if you were trying to calculate the mean, median, and average. With more complicated functions, there is room for development bugs.

There is a third technique when creating Excel WorkSheets. This technique involves embedded Excel formulas within the Excel WorkSheets. These formulas can do calculations like sum and average. This technique has the same advantage as the second technique, namely it doesn't need to go back to the SQL Server for additional queries. Plus, it removes the formula development from the Active Server page developer.

Finally the big advantage is that it removes the complicated formulas from being run on the server and instead, runs them on the client. Since the formulas are not run until the browser loads the data stream in Excel, the server need not do the work of running the formulas as seen in the second technique.

Excel 97
One of the great advantages of displaying your data in Excel is that it is Excel. This means that you get all the advantages of working in Excel, like adding charts, and complicated algorithms in the Excel WorkSheet. Plus, others who are knowledgeable in Excel, such as Sales and Marketing organizations, can save the Excel WorkSheet as an Excel document and manipulate the numbers or graphs to reflect their needs.
Changing the Mime Type
To change the mime type of the response, add the following to the top of your Active Server page.
<%
Response.ContentType = "application/vnd.ms-excel"
%>
An Excel Table with Embedded Formulas
The layout of the Excel WorkSheet will resemble the HTML table that you pass down. The most common mistake is to have HTML, HEAD, TITLE, and BODY tags in your Active Server page. You do not need these tags in your Active Server page. In fact, the WorkSheet will not open correctly with these tags. Here is an example of an Active Server page that will open as an Excel WorkSheet.

<%
Response.ContentType = "application/vnd.ms-excel"
%>
<TABLE>
<TR>
<TD>
<!-- Cell : A1 -->
2
</TD>
</TR>
<TR>
<TD>
<!-- Cell : A2 -->
3
</TD>
</TR>
<TR>
<TD>
<!-- Cell : A3 -->
=SUM(A1:A2)
</TD>
</TR>
</TABLE>

Notice the Excel formula in Cell A3. You can use any supported functions just like SUM is being used here.
Summary
The example above is pretty simple, however it gives you a good idea of what is going on. From here you should be able to use ADO to construct a table based on a results set from your database. Once the table is constructed and passed back to Excel it will appear as an Excel WorkSheet.


Next: Creation and Maintenance of Appealing ASP Pages with Dynamically Generated Contents

Previous: Simple ASP Chat

Index: Active Server Pages\IIS
   

[print version of this page]

Of Interest
Tools of the Trade








 Intranet News 
* Intranet/Internet Product Summary: January 04, 2001
* Red Herring Picks Plumtree for Employee Portal
* Tideworks Goes with Intranet from Eprise
* Crayfish Nabs 47 Percent of Intranets.com's Japan Arm
* IntraNet Solutions Notches Belt with Oakwood
* BT Secures Corporate Portal Help from Plumtree
* Mobilize Offers Enterprise App to Lexis-Nexis

More News

In' tra net - n. 1) a computer network connecting an affiliated set of clients using standard internet protocols, esp. TCP/IP and HTTP. 2) an IP-based network of nodes behind a firewall, or behind several firewalls connected by secure, possibly virtual, networks.
  IDM Unwired  
IDM on the Road
NOW AN OFFICIAL AVANTGO CHANNEL!
To easily add IDM to your PDA
click here

Featured Archive Articles

  •  Putting Your Intranet to Work: 50 Ideas for Department-Specific Applications
  •   Five-steps to implement your intranet and manage your strategic information
  •  A new generation of intranet tools intersects Customer Relationship Management and Sales Force Automation
  •   Business Intelligence and the Intranet
  •  Effective Intranet Publishing: Getting Critical Knowledge to Any Employee, Anywhere


  •   Using an Intranet to Manage 610,000 animal specimens with Java
  •  Putting JavaScript to Work for Your Intranet
  •   JavaScript Forms and Frames
  •  Introduction to the Browser Object Model
  •  Black Hole Sun: Java is Going Supernova Thanks to its Creator's Gravitational Pull


  •   Managing Large Collections of Documents: A full-length excerpt from the book "Intranet Document Management."
  •  Intranet Traffic Management: Without Quality of Service standards, intranets and extranets face death by success
  •  Issues in Intranet Security: A primer on keeping the keys to the enterprise safe


  •   Report from the trenches: Firms weigh in on intranet deployment trends
  •  The Magna Carta of the Intranet: From Smart Companies, Smart Tools
  •   Prudential HealthCare Launches Extranet to Manage Health Benefits
  •   Wells Fargo Unit Looks To Establish Its Intranet as a 'Portal'
  •   Amkor/Anam's intranet is a study in open systems integration


  •  Tutorial: Finding Your Way Around E-commerce
  •   The Essential Supply Chain
  •  A Definition of Data Warehousing
  •   Internet Messaging: from the Desktop to the Enterprise


  •  Improving Intranet Information Retrieval with Human Indexing
  •   An XML Development Kit
  •   When To Use Active Server Pages
  •  Intranets and extranets for virtual project management
  •  Why Groupware is Hard
  •  Optimizing Web Pages For Handheld Devices


  •  Version Control within a Collaborative Extranet
  •   Electronic Procurement, Extranets and You
  •  Intranet and extranets for virtual project management
  •  Overview of Virtual Private Networks


  •  What's the Future of Application Service Vendors?
  •  'Enterprise Portal' - a New Catch Phrase, But Definitions Vary
  •   Portals, You Say? This One's Private Ericsson's intranet is a give-and-take affair with employees
  •  Application Hosting

Elsewhere on internet.com
*  What Happened at XML 2000?
*  Collaborative Computing: This Changes Everything
*  inFusion: Bringing ColdFusion to a Mail Server Near Youn
*  Netscape 6, Part I: Detection and Scripting
*  AOL Introduces Mobile Communicator
*  NetMenu2
*  Bluetooth Security
*  DHTML Hiermenus 4.0: The Page-Specific Parameters
*  XML Content Syndication: Part 2
*  HardwareCentral Update: Intel's Pentium 4, a New Architecture Unveiled



Copyright 2002 Jupitermedia Corporation, All Rights Reserved.
Legal Notices | Licensing, Reprints, & Permissions | Privacy Policy | Advertising on Intranet Journal
Home | eXchange | F A Q | Find | Register |