Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Using Excel Spreadsheets as Web Data Sources

Rated 3.83 (Ratings: 4) (Add your rating)

Log in to add a comment
(5 comments so far)

Want more?

 
Picture of raines

Steven Raines

Member info | Full bio

User since: July 14, 1999

Last login: July 14, 1999

Articles written: 1

Problem

You want to create a way to easily manage a small set of data which can be easily maintained by the average office worker.

Pre-requisites

  1. Microsoft Internet Information Server 4.0
  2. Microsoft Excel ODBC Driver installed on Web Server.
  3. Microsoft ActiveX Data Objects version 2.0 or greater installed on Web Server.
  4. Microsoft Excel 95, 97 or 2000

Solution

Create an Excel Spreadsheet that contains the data. Allow the user to update this via FTP, or have a scheduled process that does this for them. Then create an Active Server Pages script which uses ActiveX Data Objects to gather the information from the spreadsheet and display it.

Example

To illustrate this solution, we'll create an online event listing. We want this to be updated by the events coordinator, who is not very web saavy.

Create the Excel Spreadsheet

To begin, create a new Worksheet in Microsoft Excel. In the first row, place the names you would like for your fields. In this example, we need to know the name of the event, the location of the event, who is coordinating it and the date and time of the event.

Figure 1

Now, we need to fill in some data.

Figure 2

Now, highlight the the columns that contain your data.

Figure 3

From the menu, Select Insert, Name, Define. In the dialog box that appears, enter the name for the highlighted data. In this example, we'll call it "Events".

Figure 4

Hit OK. You should see the name of your data group in the "Name Box" at the upper left hand corner of the screen.

Figure 5

Now your data should be all set. If your worksheet is protected or is placed in a secure directory, make sure that the user that the web server is running as (usually IUSR_MachineName) has the appropriate permissions. Save your data file and place it on your web server.

Create the ASP code

The first thing that we have to do is create the Connection and Recordset that we will be using.

'## Create Connection and Recordsets
    Dim Conn, RS
    Set Conn = CreateObject("ADODB.Connection")
    Set RS = CreateObject("ADODB.Recordset")

Next, lets create a variable for the file name. In this example, let's say we named the file "EventData.xls".

'## The Excel File Name
    XLFile = "F:\webdata\EventData.xls"

Now, we have to define what data we want to get. Most of the time, we'll want all the data. This case is no exception, so we create a SQL query to get everything from the data we defined as "Events".

'## The Data to extract
    szSQL = "select * from Events"

To get the data, we need to open the connection to the data source. Here is where we stick in the name of the file defined above. You could just as easily put the file in this line, as well.

'## Create and open the connection
    Conn.ConnectionString = "DBQ=" & XLFile & ";DRIVER=Microsoft Excel Driver (*.xls);UID=admin;"
    Conn.Open

Once this is done, we set some properties on the recordset and use the Recordset.Open method to get the specific data from our spreadsheet. By default, ADO creates Forward Only recordsets. We'll make ours static. After we get the data, we can disconnect the Active connection, since we don't need it anymore.

'## set the cursor to be static.
    rs.cursortype = 3 ' adStatic.

'## open the recordset
    rs.open szSQL, conn

'## Disconnect recordset, eliminate connection
    rs.activeconnection = nothing
    Conn.Close
    Set Conn=nothing

Now we can operate on the data just like any other recorddata. Here, we will iterate through the data, displaying the events on the web. Naturally, we could generate any formatting we want.

'## iterate through the recordset.
while not rs.eof
  response.write RS("name") & " -- "
  response.write RS("location")  & " -- "
  response.write RS("Coordinator")  & " -- "
  response.write RS("Date")  & " -- "
  response.write RS("Time")  & "<BR>"
  rs.movenext
wend

Finally, we close out the recordset and release it. Of couse, you could skip this set and move the data into the user's session, if they were going to be querying it repeatedly. For now, we'll assume that isn't the case.

RS.close
Set RS=nothing

This should get you started. Of course there are all kinds of neat things you can do once your data is imported into an ADO Recordset. Excellent information is available from the Able Consulting's Technology Page.

References

For more information, see the Microsoft Knowledge Base Article: HOWTO: Query and Update Excel Data Using ADO From ASP.

This article originally posted at Algonquin Studios.

Submitted by raster on March 30, 2000 - 08:21.

I did a super low-tech thing similar to this. The client keeps stats in an Excel document, with very simple structure, so I just have him export his files as tab-delimted text. From there he uses an upload form to upload 10 different files to the server. Then perl does it's magic, and spits out html. A server side inclue then pulls each file into the right page. This will work on any server that has perl available. Alternately, the code could probably be rewritten in the language of choice in a few hours...

login or register to post comments

Submitted by joshua on November 16, 2000 - 14:06.

Has anybody been able to pull this off in ColdFusion?

login or register to post comments

Parsing Excel files with Perl

Submitted by jma on November 2, 2002 - 01:12.

Data can be extracted from pure Excel (v95-2000) files using Perl. With the modules like XML::Excel and Spreadsheet::ParseExcel one can easily convert data in Excel spreadsheet to more usable forms. No need to teach users to define names or export to text, which is not that easy if the document contains Unicode.

login or register to post comments

This in Cold Fusion

Submitted by themango69 on March 12, 2004 - 18:36.

The top portion of this code is something I got off of another site and I cannot remember the link to it, but the comments explain themselves. The bottom portion is done almost exactly like this page shows in ASP and it works properly! Thanks alot whomever wrote this as it was the first one that showed me how to do it properly in another language that I could related to Cold Fusion (which I am a COMPLETE BEGINNER IN). SELECT * FROM Events #test.Name# #test.Location# #test.Coordinator# #test.Date# #test.Time#

login or register to post comments

Upload Excel and parse during upload

Submitted by frthorx on February 25, 2005 - 05:50.

My customer would like to have a browse/upload function to upload a .csv file that spits out html on the other end. Has anyone seen this type of function anywhere - Thanks

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.