下载Excel在ASP中无法正常工作

时间:2014-11-19 09:15:38

标签: asp-classic

我是ASP的新手。我需要在ASP中编写一个下载Excel的脚本。我试过但它正在下载整个页面内容,但我需要从数据库下载该表。

这是我的代码:

     <%@Language="VBScript"%>
     <form name="form1" id="form1" method="post">
     <input type="hidden" name="action" value="sel">
     <table>
     <tr>
     <td><input type="submit" name="submit" id="submit" value="Download Excel"></td>
     </tr>
     </table>
     Hello World
     <%
     action = Request.Form("action")

     If action="sel" Then
     Response.ContentType = "application/octet-stream"
     Response.ContentType = "application/vnd.ms-excel"

     SET Conn = Server.CreateObject("ADODB.Connection")
     Conn.OPEN "PROVIDER=SQLOLEDB;DATA  SOURCE=10.1.1.1;UID=sa;PWD=root;DATABASE=Student"
     dim Conn,Rs
     set Rs=server.createobject("ADODB.recordset")
     Rs.open "SELECT * FROM studentdetails",Conn
     Response.AddHeader "Content-Disposition", "attachment; filename=xl_data.xls"
     %>
   <TABLE BORDER=1>
       <TR>
       <%
       j = 2
       For i = 0 to RS.Fields.Count - 1
       %>
       <TD width="18"><B>
       <% = RS(i).Name %></B></TD>
       <% Next %>
       <TD width="42"></TD>
       <TD width="53"></TD>
       </TR>
       <%
       Do While Not RS.EOF
       %>
      <TR>
      <% For i = 0 to RS.Fields.Count - 1
       %>
      <TD VALIGN=TOP><% = RS(i) %></TD>
      <% Next %>
      </TR>
      <%
      RS.MoveNext
      j = j + 1
      Loop
      RS.Close
  End If
      %>
      </TABLE>

在这个程序中我已经包含了Hello World系列,下载时也是下载的。所以请给我一些建议。提前了。

1 个答案:

答案 0 :(得分:1)

替换

Response.ContentType = "application/octet-stream"

Response.Clear

否则您将表格和表格发送到Excel