Writing an xls file to the browser

I'm having trouble reading a file on the server, then writing the file to the browser.  I keep getting a file error in excel.  Here's what I'm currently doing:

<%
Response.buffer = TRUE
strFileName = Server.MapPath("../../securedocuments/myFile.xls")
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile(strFileName, 1, false)
Response.ContentType = "application/x-msexcel"
Response.AddHeader "Content-disposition", "attachment; filename=myFile.xls"

' loop and write
Dim strLine
Do While Not file.AtEndOfStream
      strLine = file.readline & vbcrlf
      Response.BinaryWrite strLine
Loop

' close everything
file.close
Set file = nothing
Set fs = nothing

Response.End
%>

If anyone has any ideas, please help! ;)

Thanks,

davidgkyk
davidgkyAsked:
Who is Participating?
 
nurbekConnect With a Mentor Commented:
maybe you need to read from excel file like that?

Dim excRS, excConn
Dim ConnStr

ConnStr = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & Server.MapPath("test.xls")

Set excConn = Server.CreateObject("ADODB.Connection")
excConn.Open ConnStr

Set excRS = excConn.Execute("SELECT * FROM [Sheet1$]")

If Not excRS.EOF Then
   excRS.MoveFirst
   Response.Write "<table border=""1"">"
   Response.Write("<tr bgcolor=#CCCCCC><td>ID</td><td>Ad</td><td>Soyad</td></tr>")
   Do Until excRS.EOF
      Response.Write "<tr>"
      Response.Write "<td>&nbsp;" & excRS("ID") & "</td>"
      Response.Write "<td>&nbsp;" & excRS("Name") & "</td>"
      Response.Write "<td>&nbsp;" & excRS("Surname") & "</td>"
      Response.Write "</tr>"
      excRS.MoveNext
   Loop
   Response.Write "</table>"
End If

Set excRS = Nothing
excConn.Close
Set excConn = Nothing
0
All Courses

From novice to tech pro — start learning today.