Search code examples
asp-classicexport-to-excel

Exporting just the html table in a classic asp page


we had some demands here in my job that include working with asp. We are not experient with that and I could not find a solution to this problem in the other topics here in stackoverflow

the thing is, if I put

<%  Option Explicit
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
%>

the whole page goes to the excel file.

and I want only one table to be in the generated xls file.

the table I want is beeing generated by this line:

<% =Result %>

how can that be achieved?

thxxx


Solution

  • You'll need something in your URL that makes the request for the XLS version distinct from the standard version.

    Typically you do this with a query string value so your URL becomes http://yoursite.com/yourpage.asp?mode=xls.

    Now in your code you can use:

    <%
    Option Explict
    Dim result
    
    ''# Build Table into result variable
    
    If Request.QueryString("mode") = "xls" Then 
        Response.ContentType = "application/vnd.ms-excel"    
        Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls" 
    Else
    %>
    <html>
        <!-- Normal HTML before the table here -->
    <%
    End If
    
    Response.Write result
    
    If Request.QueryString("mode") <> "xls" Then
    %>
        <!-- Normal HTML after the table -->
    </html>
    <%
    End If
    %>
    

    In you include in your page a line back to itself with the extra ?mode=xls on the end the user will get excel launched with just the table loaded into it.