Labels

Thursday, September 9, 2010

output Excel using classic ASP

Private Sub DownloadFile(file) 
     Dim strAbsFile 
     strAbsFile = server.MapPath("..\DownloadFiles") & "\" & file
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
     If objFSO.FileExists(strAbsFile) Then 
         Set objFile = objFSO.GetFile(strAbsFile) 
         Response.Clear 
        Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name 
         'The following line not working on IIS7 & Windows2008
         'Response.AddHeader "Content-Length", objFile.Size 
         'Response.ContentType = "application/octet-stream" 
         Response.ContentType="application/x-msexcel"
         Set objStream = Server.CreateObject("ADODB.Stream") 
         objStream.Open 
         '-- set as binary 
         objStream.Type = 1 
         Response.CharSet = "UTF-8" 
         '-- load into the stream the file 
         objStream.LoadFromFile(strAbsFile) 
         '-- send the stream in the response 
         Response.BinaryWrite(objStream.Read) 
         objStream.Close 
         Set objStream = Nothing 
         Set objFile = Nothing 
     Else 'objFSO.FileExists(strAbsFile) 
         Response.Clear 
         Response.Write("No such file exists.") 
     End If 
     Set objFSO = Nothing 
 End Sub 

No comments:

Post a Comment