I’m trying to set up a page that downloads a sql table and gives the user a prompt to save it to excel. This will be used in the horrible thing called IE7.
The problem is that instead of downloading the file that I know exists, it generates the asp page into the xls file, and titles it “DataManager.xls”. So if I open that, I get the header bar of the asp page, and if I leave out the resonse.end I get the page body and footer. But all my research says this code should be downloading an existing file, not creating one.
None of my response.write tracers work. But strSQL is being run properly, because that generates the file.
dim strSQL
if(Request.QueryString("submitbutton") = "Download") then
dim fn
dim FPath
strSQL = "EXECute [TABLE2EXCEL] 'S007\SQLSRV','" & UCase(request.QueryString("SelTABLE")) & "','" & UCase(request.QueryString("SelTABLE")) & "','" & Replace(Request.ServerVariables("LOGON_USER"),"CORP\","") & "'"
fn = cnt.execute(strSQL)
response.write strSQL
response.write fn
response.write FPath
Response.ContentType = "application/vnd.ms-excel" ' arbitrary
FPath = fn
Response.AddHeader "Content-Disposition","attachment; filename=ASSOCIATEROSTERFeb102012909AMNBKMZEJ.xls" '& fn
Set adoStream = CreateObject("ADODB.Connection")
adoStream.Open()
adoStream.Type = 2
adoStream.LoadFromFile("\\s007\folder\DataFactory\ASSOCIATEROSTERFeb102012909AMNBKMZEJ.xls")'(FPath)
Response.BinaryWrite adoStream.Read()
adoStream.Close
Set adoStream = Nothing
Response.End
end if
And just to be complete on data, here’s my sql code:
ALTER procedure [dbo].[TABLE2EXCEL]
(
@db_name varchar(100),
@table_name varchar(100),
@file_name varchar(100),
@NBK varchar(10)
)
as
--Generate column names as a recordset
declare @columns varchar(8000), @sql varchar(8000), @data_file varchar(100)
select
@columns=coalesce(@columns+',','')+column_name+' as '+column_name
from
information_schema.columns
where
table_name=@table_name
select @columns=''''''+replace(replace(@columns,' as ',''''' as '),',',',''''')
declare @TimeStamp varchar(50)
SELECT @TimeStamp = Replace(Replace(CAST(CONVERT(datetime,getdate())as varchar),':',''),' ','') + @NBK
print @TimeStamp
--Set folder for output file
set @file_name = '\\s007\folder\DataFactory\' + @file_name + @TimeStamp + '.xls'
--Create a dummy file to have actual data
select @data_file='\\s007\folder\DataFactory\data_fildat' + @TimeStamp + '.xls'--substring(@file_name,1,len(@file_name)-charindex('\',reverse(@file_name)))+'\data_filez.xls'
print @data_file
--Generate column names in the passed EXCEL file
set @sql='exec master..xp_cmdshell ''bcp " select * from (select '+@columns+') as t" queryout "'+@file_name+'" -c -S S007\SQLSRV -U userlogin -P passwordisbestpassword -k'''
exec(@sql)
--Generate data in the dummy file
set @sql='exec master..xp_cmdshell ''bcp " select * from '+@table_name+'" queryout "'+@data_file+'" -c -S S007\SQLSRV -U userlogin -P passwordisbestpassword -k'''
exec(@sql)
PRINT @sql
PRINT @data_file
--Copy dummy file to passed EXCEL file
set @sql= 'exec master..xp_cmdshell ''type "'+@data_file+'" >> "'+@file_name+'"'''
exec(@sql)
--Delete dummy file
set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''
exec(@sql)
return @file_name
Usually this is done by generating a regular HTML table with the data in it and sending out the Excel MIME type.
(then output the HTML table)
See this question for an example: Export data to excel file from Classic ASP failing