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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export recordset from query to Excel spreadsheet 2

Status
Not open for further replies.

djmc

Programmer
Jun 12, 2002
179
CA
I have an asp page which shows a formatted view (html table) of a record set generated from a query in Access via ADO. I would like to have that same recordset in Excel spreadsheet form so the user can click on a button and download it. Are there any examples on how to do that which someone can provide me? Thanks.
 
if you search this forum I'm sure you'll find an answer. This questions has been posted many times...

tonycomment.gif


.....................................................................................................................
"The secret to creativity is knowing how to hide your sources."
-Albert Einstein

 
Add this line to the top of your asp page and then have the page build a table to display the results.

Response.ContentType = "application/vnd.ms-excel"

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
I have read the posts on the forum regarding the conversion of a recordset to Excel spreadsheets.

Here is my code for it:

Function getExcelFile(ByRef strDbName, ByRef strDbPass, ByRef strSQL, ByRef strFileName)

Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName & ";"
Response.ContentType = "bad/type"

' Create the database connection
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath(strDbName) & ";" _
& "Jet OLEDB:Database Password=" & strDbPass & ";"

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, conn

Dim i
For i = 0 to rs.Fields.Count - 1
Response.Write rs.Fields(i).Name & chr(09)
Next

' -- End the line so your row is complete; use the built-in constant 'vbnewLine'
Response.Write vbNewLine
' -- Then send the entire data set

Do While Not rs.EOF
For i = 0 to rs.Fields.Count - 1
Response.Write rs.Fields(i) & chr(09)
Next
' -- End the line so your row is complete
Response.Write vbNewLine
' -- Next record
rs.MoveNext
Loop

rs.close
conn.close
Set rs = Nothing
Set conn = Nothing
End Function
--------------------------------------
Now it works but the only problem I have with this is that the file is not a native Excel file. It is using tab delimited format right now. I want to know how I can set the file to native Excel format (without opening the tab delimited file and save as Excel worksheet). Thanks
 
with objCmd
.commandText = "br_sp_threeDayReport"
.CommandType = &H0004
End With

set objrs = objcmd.execute()
recCount = 0

do while not objrs.eof
if lcase(objrs(&quot;created&quot;)) <> &quot;null&quot; then
if lastName <> &quot;&quot; then
if lastName <> objrs(&quot;lastName&quot;) then strOut = strOut & &quot;<tr></tr>&quot; '<
end if
recCount = recCount + 1
lastName = objrs(&quot;lastName&quot;)
end if
strOut = strOut & objrs(0)
objrs.movenext
loop

if recCount > 0 then ' <'records found'

Set act = fso.CreateTextFile(server.mappath(g_filename), true)
with act
.WriteLine &quot;<html xmlns:x=&quot;&quot;urn:schemas-microsoft-com:eek:ffice:excel&quot;&quot;>&quot;
.WriteLine &quot;<head>&quot;
.WriteLine &quot;<!--[if gte mso 9]><xml>&quot;
.WriteLine &quot;<x:ExcelWorkbook>&quot;
.WriteLine &quot;<x:ExcelWorksheets>&quot;
.WriteLine &quot;<x:ExcelWorksheet>&quot;
.WriteLine &quot;<x:Name>Items Over 3 Days Old</x:Name>&quot;
.WriteLine &quot;<x:WorksheetOptions>&quot;
.WriteLine &quot;<x:pageSetup>&quot;
.WriteLine &quot;<x:Header>BRTT Active Requests Over 3 Days Old</x:Header>&quot;
.WriteLine &quot;</x:pageSetup>&quot;
.WriteLine &quot;<x:print>&quot;
.WriteLine &quot;<x:ValidPrinterInfo/>&quot;
.WriteLine &quot;</x:print>&quot;
.WriteLine &quot;</x:WorksheetOptions>&quot;
.WriteLine &quot;</x:ExcelWorksheet>&quot;
.WriteLine &quot;</x:ExcelWorksheets>&quot;
.WriteLine &quot;</x:ExcelWorkbook>&quot;
.WriteLine &quot;</xml>&quot;
.WriteLine &quot;<![endif]--> &quot;
.WriteLine &quot;</head>&quot;
.WriteLine strOut '<-- this is an HTML table
end with
act.close


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Am I missing something, or are you guys making this a lot more complicated than it needs to be?

Just add this line to the <head> section of your page:
Response.ContentType = &quot;application/vnd.ms-excel&quot;

Then in the <body> section of the page, layout the data from the recordset in an HTML table.

The page will open in the browser as an Excel spreadsheet. If the user wants to save it to their local machine, all they have to do is click File/Save As... and give it a path/filename.


_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Rott,

I've used my code to create tables to append to emails without ever using the browser to display it. I just depends on what you are trying to do.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
djmc wrote:
&quot;...shows a formatted view (html table) of a record set...&quot;
&quot;...would like to have that same recordset in Excel spreadsheet form so the user can click on a button and download it.&quot;


It seems to me that the bulk of the work was already done and all that was needed was that one line of vbscript code in the head section.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
This is what I was trying to do:

Function getExcelDownload(ByRef strDbName, ByRef strDbPass, ByRef strSQL, ByRef strFileName, ByRef strName, ByRef strHeader)

' Force download prompt
Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; & strFileName & &quot;;&quot;
Response.ContentType = &quot;bad/type&quot;

' Create the database connection
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)

conn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; _
& &quot;Data Source=&quot; & Server.MapPath(strDbName) & &quot;;&quot; _
& &quot;Jet OLEDB:Database Password=&quot; & strDbPass & &quot;;&quot;

Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open strSQL, conn

Dim i, strOut, recCount

strOut = &quot;<table border='1'>&quot;
strOut = strOut & &quot;<tr>&quot;
For i = 0 to rs.Fields.Count - 1
strOut = strOut & &quot;<td><b>&quot; & rs.Fields(i).Name & &quot;</b></td>&quot;
Next
strOut = strOut & &quot;</tr>&quot;

Do While Not rs.EOF
strOut = strOut & &quot;<tr>&quot;
For i = 0 to rs.Fields.Count - 1
strOut = strOut & &quot;<td align='left'>&quot; & rs.Fields(i) & &quot;</td>&quot;
Next
strOut = strOut & &quot;</tr>&quot;
recCount = recCount + 1
rs.MoveNext
Loop
strOut = strOut & &quot;</table>&quot;

' Close the database connection
rs.close
conn.close
Set rs = Nothing
Set conn = Nothing

' Generate XML Excel Format
If recCount > 0 Then
Response.Write &quot;<html xmlns:x=&quot;&quot;urn:schemas-microsoft-com:eek:ffice:excel&quot;&quot;>&quot;
Response.Write &quot;<head>&quot;
Response.Write &quot;<!--[if gte mso 9]><xml>&quot;
Response.Write &quot;<x:ExcelWorkbook>&quot;
Response.Write &quot; <x:ExcelWorksheets>&quot;
Response.Write &quot; <x:ExcelWorksheet>&quot;
Response.Write &quot; <x:Name&quot; & strName & &quot;</x:Name>&quot;
Response.Write &quot; <x:WorksheetOptions>&quot;
Response.Write &quot; <x:pageSetup>&quot;
Response.Write &quot; <x:Header&quot; & strHeader & &quot;</x:Header>&quot;
Response.Write &quot; </x:pageSetup>&quot;
Response.Write &quot; <x:print>&quot;
Response.Write &quot; <x:ValidPrinterInfo/>&quot;
Response.Write &quot; </x:print>&quot;
Response.Write &quot; </x:WorksheetOptions>&quot;
Response.Write &quot; </x:ExcelWorksheet>&quot;
Response.Write &quot; </x:ExcelWorksheets>&quot;
Response.Write &quot;</x:ExcelWorkbook>&quot;
Response.Write &quot;</xml>&quot;
Response.Write &quot;<![endif]--> &quot;
Response.Write &quot;</head>&quot;
Response.Write strOut '<-- this is an HTML table
End If
End Function
%>

Basically I wanted an Excel spreadsheet created from a query in access and bring up the download dialog for the user to save onto the hard drive. It appears that the XML format works fine so I am keeping it in this way. Thanks.
 
If you're happy, I'm happy

[peace]

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Just want to clarify as I have seen it written both ways in posts. Does this code <% Response.ContentType = &quot;application/vnd.ms-excel&quot; %> actually OPEN the Excel program or does it just appear in the browser in Excel format and give you the ability to &quot;Save As&quot; Excel?

Thanks!

Toni
 
<% Response.ContentType = &quot;application/vnd.ms-excel&quot; %> tells IE to use the excel plugin to display the page - it does not open the excel program.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
This does work, but when you save it as an Excel workbook, unfortunately any number data types in the spreadsheet does not &quot;act&quot; like a number and can't be summed or treated in any way as a real number. Does anyone know how to get around this?
 
When I set the ContentType to excel, the numbers do act like numbers.

I tried opening a web page that does this on several computers and the number fields acted like numbers on all of them.

BTW....I haven't figured out the rhyme or reason, but on one of my computers, this actually does open the Excel application instead of opening the spreadsheet in IE.....


_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Hi all,

I am using the following code to fetch userids from a database and display it in browser in MS Excel worksheet format.
The problem is when i run the code for the first time; it does not show me the results. It works better when I run the same page again while the first instant is not closed (i.e. if I did not close the first browser window).

1. How can I get the results in the first instant of the page?
2. How can I enforce the code to display the Open or Save options dialog box.?

Can anybody help ?


My Code is given below


<%
Response.ContentType = &quot;Application/vnd.ms-excel&quot;

' connection code here

Set rsUsers = Server.CreateObject(&quot;ADODB.RecordSet&quot;)
sql = &quot;SELECT UserID FROM Users&quot;
rsUsers.Open sql,connection


Response.Write(&quot;<TABLE>&quot;)
Response.Write(&quot;<TR>&quot;)
Response.Write(&quot;<TD>User ID</TD>&quot;)
Response.Write(&quot;</TR>&quot;)
While Not rsUsers.EOF
Response.Write(&quot;<TR>&quot;)
Response.Write(&quot;<TD>&quot; & rsUsers.Fields(&quot;UserID&quot;) & &quot;</TD>&quot;)
rsUsers.MoveNext
Response.Write(&quot;</TR>&quot;)
Wend

Response.Write(&quot;</TABLE>&quot; )

rsUsers.Close
Set rsUsers=Nothing

%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top