faqts : Computers : Programming : Languages : Asp : ASP/VBScript : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 59 people (47%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I download the information from the asp tables into excel?

Apr 4th, 2003 08:18
Joe Roulette, unknown unknown, Jayanta Daspurkayastha


Try the following way:

1.  Create a FileSystemObject:
        Set oFS = Server.CreateObject("Scripting.FileSystemObject")

2. Create a TextFile as follows (type is specified by the file's 
extension):
   Set oTS = oFS.CreateTextFile("c:\test.csv", True, False)

3.  Next, I run the SQL to select the data from the table into a
RecordSet--use your code over here.

4.  Write to the TextFile, the header for the columns as follows:
        oTs.WriteLine strHeader  where strHeader is a String listing the
column names separated by commas.

5.  Then, I use GetString function to write the Records from the 
RecordSet into the TextFile.
        oTS.WriteLine oRs.GetString(,-1,",",vbCrLf)


Here's another way to export a table into Excel:

1. In the ASP page that includes the table, put the following at the 
top of the ASP code:

<%@ LANGUAGE="VBSCRIPT" %>
<%
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content Disposition", "attachment;filename=f.xls"
' The AddHeader line forces the save/open box
%>

2. Note that in the example, "attachment;filename=f.xls" an Excel file 
named "f.xls" is being generated. The browser should prompt the user 
with a dialog box asking whether to save the document or open it. 
If 'open it' is chosen, Excel opens and loads the table.

3. It's helpful to have only the table being exported on the page, so 
if you have a bunch of other stuff, like menus or images, it may be 
helpful to have a link that points to a secondary page, which includes 
the table being exported - and only export the secondary page...

4. I know this works with MS IE 5+, not sure about any other browsers.

-- JR



© 1999-2004 Synop Pty Ltd