Export to Excel from ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ss_learner
    New Member
    • Nov 2005
    • 2

    Export to Excel from ASP

    Hi there ,
    Can anyone help me out!!

    I have a report (basically retrieving of records from the database and show it in a htm table format)
    which is displayed on the browser as htm table..
    Now my problem comes here..
    I was asked to program such that the report is shown in excel rather than as html table..
    with great enthusiasm I started working only to find out i am not able to do..

    when I click on the button, the excel is opening, but only that it is completely blank and not showing any records
    if i turn off content type, then I am able to display in html table..
    Pls help me out!!
    the delivarable is only a day away:(
    I am posting my code here :
    on top of page i added this
    Code:
     <% 
    Response.Buffer = TRUE
    Response.ContentType = "application/vnd.ms-excel"%>
     
    After that the record set is retreived like this:
    <tr valign=top>	
    <% while iCnt <=7%>
    <td >		
    <table border="1" cellpadding="0" cellspacing="0" width=100% class=formtable>
    <%str = "Exec sp_printanydate '" & cdate(request("StartDate"))& "', '" & request.QueryString("cid") & "', "& iCnt&" " 
    rs.Open str,conn,1,3
    while not rs.EOF %>
    <tr valign=top >	
    <td valign=top bgcolor=<%=color_value%> >
    <%if isnull(rs("subjectname")) then%>
    		-
    <%else%>
    <%=rs("subjectname")%>
    <%end if %>	<br>		
    <%if isnull(rs("classname")) then%>
    	- 
    <%else%>
    <%=rs("classname")%>
    <%end if %>	<br>		
    <%=rs("fromtime")%> - <%=rs("totime")%><br>
    <%if isnull(rs("room")) then%>
    - 
    <%else%>
    <%=rs("room")%>
    <%end if %>		<br>	
    </td>	
    </tr>				
    <%
    rs.MoveNext		
    wend
    rs.Close
    %>						
    </table>		
    </td>	
    <%iCnt=iCnt+1
    wend%>
    </tr>		
    </table>
    Thanks very much in advance
    Regards
    Last edited by Niheel; Nov 25 '05, 04:17 AM.
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    Here is an example on the net:
    Export to Excel using ASP

    Comparing yours with the sample, it seems you forgot the AddHeader line:

    Code:
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment; filename=Sample_File.xls"
    niheel @ bytes

    Comment

    • thema3x
      New Member
      • Nov 2007
      • 1

      #3
      I have this code, and works with no problem.

      Code:
      <%@ Language=VBScript %>
      <% Response.ContentType="application/vnd.ms-excel" %>
      <Table>
      
      <TR>
      <TD>Col1</TD>
      <TD>Col2</TD>
      <TD>Col3</TD>
      <TD>Total</TD>
      </TR>
      
      <TR>
      <TD><div align="center" class="style1">10</div></TD>
      <TD>20</TD>
      <TD>2</TD>
      <TD>=sum(a2:b2)</TD>
      </TR>
      
      <TR>
      <TD>30</TD>
      <TD>40</TD>
      <TD></TD>
      <TD>=sum(a3:b3)</TD>
      </TR>
      </TABLE>

      Comment

      • mady1380
        New Member
        • Sep 2006
        • 18

        #4
        Hi there,

        I have tired this
        Code:
         Response.ContentType = "application/vnd.ms-excel" 
        Response.AddHeader "Content-Disposition", "attachment; filename=Enquiry_List.xls"
        Response.Charset="UTF-8"
        which works fine for me. My only problem is that, i am not getting my data in UTF-8 format. Any idea? I have searched many articles on export to excel in ASP. I have not found either in one anything regarding the Encoding issue.

        Any help would be appreciated.

        Thanks,
        Menon

        Comment

        Working...