Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Export to excel
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

kolucoms6
Average Member

845 Posts

Posted - 14 June 2007 :  11:24:31  Show Profile

I am using below code to Export few datas to excel.But It simply display the data instead of opening the same in excel.Kindly guide me.



<%

Set adRs=Server.CreateObject("ADODB.Recordset")

sSql="SELECT * from qryDataDisplay"

adRs.Open sSql,cn,3,3

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=List.xls"

if Not adRs.EOF then
response.write "<table border=1>"
Do until adRs.EOF
response.write "<tr>"
for i = 0 to adRs.Fields.Count-1
response.write "<td>" & adRs.fields(i).Value & "</td>"
Next
response.write "</tr>"
adRs.movenext
Loop
response.write "</table>"
end if

%>

AnonJr
Moderator

United States
5768 Posts

Posted - 14 June 2007 :  16:10:10  Show Profile  Visit AnonJr's Homepage
I use this code in an app I wrote for work. You need to add some extra stuff in the <head>. You may need to adjust some of it according to what you are doing.
	 Case "excel"
'Returns the result as an Excel spread sheet.
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">" & vbNewLine & _
"<head>" & vbNewLine & "<style>" & vbNewLine & _
"<!--table" & vbNewLine & _
"@page" & vbNewLine & _
"{mso-header-data:""&CLog Results for " & strName & "\000A" & _
"From " & Replace(strFrom, "/", "-") & " to " & Replace(strTo, "/", "-") & "\000A" & _
"Printed\: &D\000APage &P"";}" & vbNewLine & _
"br" & vbNewLine & _
"{mso-data-placement:same-cell;}" & vbNewLine & _
"-->" & vbNewLine & "</style>" & vbNewLine)
Response.Write("<!--[if gte mso 9]><xml>" & vbNewLine & _
"<x:ExcelWorkbook>" & vbNewLine & _
"<x:ExcelWorksheets>" & vbNewLine & _
"<x:ExcelWorksheet>" & vbNewLine & _
"<x:Name>Log Results for " & strName & "</x:Name>" & vbNewLine & _
"<x:WorksheetOptions>" & vbNewLine & _
"<x:Print>" & vbNewLine & _
"<x:ValidPrinterInfo/>" & vbNewLine & _
"</x:Print>" & vbNewLine & _
"</x:WorksheetOptions>" & vbNewLine & _
"</x:ExcelWorksheet>" & vbNewLine & _
"</x:ExcelWorksheets>" & vbNewLine & _
"</x:ExcelWorkbook>" & vbNewLine & _
"</xml><![endif]-->" & vbNewLine & _
"</head>" & vbNewLine & "<body>" & vbNewLine)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.22 seconds. Powered By: Snitz Forums 2000 Version 3.4.07