Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transfering asp to excel

Status
Not open for further replies.

Garabaldi

Technical User
Jan 16, 2002
61
CA
Hi,

I have a page set up in asp which pulls certain fields in a database.

I need to have that page exported into excel.

I know there is a way to do this through excel through the Grab Data method and an .iqy file.

However, whenever I try to do this i end up getting an error.

Does anyone know a way to get this to work?

Thanks!!!
 
A simple way to do it would be to create a .csv file and just write your information to it as comma-delimited text.

Each row is on a seperate line, all values are comma seperated
file is readable by excel and also (bonus) half a million other programs
 
Try this, I got it from Microsoft.



<%@ LANGUAGE=&quot;VBSCRIPT&quot; %>

<HTML>
<HEAD>
<TITLE>Create Tab Delimited Text File</TITLE>
</HEAD>
<body>

<%

'Create a randome Filename
nRandom = Int((1000000 - 1 + 1) * Rnd + 1000000)
fileExcel = &quot;t&quot; & CStr(nRandom) & &quot;.xls&quot;

'Replace 'MyWeb' with your virtual directory name or just the
'slash if it is at the filePath= Server.mapPath(&quot;\MyWeb&quot;)
filename=filePath & &quot;\&quot; & fileExcel

'Create the File with extension .xls using the FileSytemObject
'If the file does not exist, the TRUE parameter will allow it
'to be created. Make sure the user* impersonated has write
'permissions to the directory where the file is being created.

Set fs = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set MyFile = fs.CreateTextFile(filename, True)

'Open the connection and retrieve data from the database
Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;)

'The following open line assumes you have set up a System
'DataSource by the name of Pubs pointing to the Pubs database.
'And that the sa password is blank. If you use a different
'username and password, make sure that the user has permissions to
'the pubs database, and select permissions on the Authors Table.
'If you do not have SQL Server, see notes below for how to modify
'sample to work with an Access database

cn.Open &quot;DSN=Pubs;UID=sa;PWD=;DATABASE=pubs&quot;
Set rs = cn.Execute(&quot;SELECT
au_id,au_lName,au_fname,phone,address,city,state,zip,contract FROM
Authors&quot;)

strLine=&quot;&quot; 'Initialize the variable for storing the filednames

For each x in rs.fields
'Separate field names with tab so that these appear in
'different columns in Excel
strLine= strLine & x.name & chr(9)
Next
'Write this string into the file
MyFile.writeline strLine

'Retrieve the values from the database and write into the database
Do while Not rs.EOF
strLine=&quot;&quot;
for each x in rs.Fields
strLine= strLine & x.value & chr(9)
next
MyFile.writeline strLine
rs.MoveNext
Loop

'Clean up
MyFile.Close
Set MyFile=Nothing
Set fs=Nothing

'Show a link to the Excel File.
link=&quot;<A href=&quot; & fileExcel & &quot;>Open Excel</a>&quot;
Response.write link
%>

</BODY>
</HTML>


.....Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top