p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: Re: Use contenttype to let IE open execel file


Message #1 by "Rick Lynn" <rick.lynn@i...> on Thu, 31 May 2001 18:54:48
> Dear Hariharan,
> Thank you very much to spend time to help me.
> The excel files contain not only text but only graphics, therefore I am
> afraid that your temporary solution could not be applied.
> Best Regard.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> From: Le Huu Duc
> Information System Section
> Tel: 332
> E-Mail: lhduc@m...
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> ----- Original Message -----
> From: "Hariharan" <hariharan_m2001@r...>
> To: "Code Clinic" <proasp_codeclinic@p...>
> Sent: Friday, April 06, 2001 8:09 AM
> Subject: Re: Use contenttype to let IE open execel file
> 
> 
> > Hi Le,
> >   Ya.. its me again.
> > I got a temp. solution- maybe u can use EXCEL drivers and then open
> > file and display excel data using recorsets. This is the code for that-
> >
> > <%
> > ' Selected constants from adovbs.inc
> > Const adOpenStatic = 3
> > Const adLockPessimistic = 2
> >
> > Dim cnnExcel
> > Dim rstExcel
> > Dim I
> > Dim iCols
> >
> > ' This is all standard ADO except for the connection string.
> > Set cnnExcel = Server.CreateObject("ADODB.Connection")
> > cnnExcel.Open "DBQ=" & Server.MapPath("xl_data.xls") & ";" & _
> > "DRIVER={Microsoft Excel Driver (*.xls)};"
> >
> > ' FYI: TestData is my named range in the Excel file
> > Set rstExcel = Server.CreateObject("ADODB.Recordset")
> > rstExcel.Open "SELECT * FROM TestData;", cnnExcel, _
> > adOpenStatic, adLockPessimistic
> >
> > ' Get a count of the fields and subtract one since we start
> > ' counting from 0.
> > iCols = rstExcel.Fields.Count
> > %>
> > <table border="1">
> > <thead>
> > <%
> > ' Show the names that are contained in the first row
> > ' of the named range.  Make sure you include them in
> > ' your range when you create it.
> > For I = 0 To iCols - 1
> > Response.Write "<th>"
> > Response.Write rstExcel.Fields.Item(I).Name
> > Response.Write "</th>" & vbCrLf
> > Next 'I
> > %>
> > </thead>
> > <%
> > rstExcel.MoveFirst
> >
> > ' Loop through the data rows showing data in an HTML table.
> > Do While Not rstExcel.EOF
> > Response.Write "<tr>" & vbCrLf
> > For I = 0 To iCols - 1
> > Response.Write "<td>"
> > Response.Write rstExcel.Fields.Item(I).Value
> > Response.Write "</td>" & vbCrLf
> > Next 'I
> > Response.Write "</tr>" & vbCrLf
> >
> > rstExcel.MoveNext
> > Loop
> > %>
> > </table>
> >
> > <%
> > rstExcel.Close
> > Set rstExcel = Nothing
> >
> > cnnExcel.Close
> > Set cnnExcel = Nothing
> > %>
> >
> > This article was posted at-
> > http://www.asp101.com/samples/viewasp.asp?file=xl%5Fdata%2Easp
> >
> >
> > Do let me know if u get any other solutions.
> > Regards,
> > Hari
> >

Here's how I got around the problem of IIS requiring you to either open or 
save an Excel file rather than rendering the page in Excel.  Post to a 
form in the usual way but tell IIS you're passing a parameter -- don't 
even need to actually pass a param, just the "?":

<form name="preview" action="excel.asp?" method="POST">

Somehow this tricks IIS into rendering the page as an excel file, provided 
that excel.asp has this line:

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

Without the "?", IIS prompts to open/save the file.

~ Rick


  Return to Index