打印本文 打印本文  关闭窗口 关闭窗口
也谈ASP中的RESPONSE属性
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:02:05
    在随意去逛的时候,看到《ASP中巧用Response属性》这篇文章,想起以前看到的一篇国外相关的文章,不敢独占,拿出来和大家分享。
    调试是通过了,但有些用英文原话,还请大家多多包涵。
    这里讲的是用ASP页面如何转换成EXCEL表格的形式,大家一点还记得以前有大虾说过用DLL的形式来解决,但这样对于大数据量来说,实在是慢。如果用文中介绍的方法,将不占用服务器的资源,只是在客户端打开。还有个好处是:在网页中的表格显示成什么样子,在EXCEL中的表格就是什么样子。好吧,再告诉一个令人激动的消息,除了表格数据,在EXCEL中,象按钮等都一概不显示。这样又解决了大家的网页打印问题,简直就是一举多得。
    废话少说,进入正题。
    为了创建一个动态的EXCEL报表,你首先要在一个ASP文件中写:
    <%
    Response.ContentType = "application/vnd.ms-excel"
    %>
    (记住:这句话一定要写在文章的开头哟,不然的话,嘿嘿....)
这句话是告诉浏览器接下来的代码是EXCEL格式的,然后浏览器会提示你SAVE OR OPEN 该文件,然后你就可以打开了或保存它了。
    看一个例子先:
<%
Response.ContentType = "application/vnd.ms-excel"
%>
<HTML>
<HEAD>
<link rel="stylesheet" href="STYLE.CSS">
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<table border="1" width="100%" bordercolorlight="#000080" bordercolordark="#0000FF" height="190">
  <tr>
    <td width="100%" colspan="12" height="30">
      <p align="center"><b>由ASP页面转换成EXCEL报表</b></td>
  </tr>
  <%for i=1 to 8%>
  <tr>
    <td width="8%" height="16" align="center"><%=1*i%></td>
    <td width="8%" height="16" align="center"><%=2*i%></td>
    <td width="8%" height="16" align="center"><%=3*i%></td>
    <td width="8%" height="16" align="center"><%=4*i%></td>
    <td width="8%" height="16" align="center"><%=5*i%></td>
    <td width="8%" height="16" align="center"><%=6*i%></td>
    <td width="8%" height="16" align="center"><%=7*i%></td>
    <td width="8%" height="16" align="center"><%=8*i%></td>
    <td width="9%" height="16" align="center"><%=9*i%></td>
    <td width="9%" height="16" align="center"><%=10*i%></td>
    <td width="9%" height="16" align="center"><%=11*i%></td>
    <td width="9%" height="16" align="center"><%=12*i%></td>
  </tr>
  <%next%>
  <tr>
   <td>=sum(a2:a9)</td>
    <td>=sum(b2:b9)</td>
    <td>=sum(c2:c9)</td>
    <td>=sum(d2:d9)</td>
    <td>=sum(e2:e9)</td>
    <td>=sum(f2:f9)</td>
    <td>=sum(g2:g9)</td>
    <td>=sum(h2:h9)</td>
    <td>=sum(i2:i9)</td>
    <td>=sum(j2:j9)</td>
    <td>=sum(k2:k9)</td>
    <td>=sum(l2:l9)</td>
    
  </tr>
</table>
<P align=center><input type=button value="test report" onclick="javascript:alert('hello , my friend,are you ready?')"></P>

</BODY>
</HTML>
把此代码搞到你的机器上,看看效果如何,哈哈,你就偷着乐去吧
等等,还有个小问题:
  Microsoft has acknowledged a BUG in IE (3.02, 4.0, 4.01, 4.01sp1) which causes it to misinterpret Excel output, particularly when generated by ASP, ISAPI, or CGI. You can read more about it at http://support.microsoft.com/support/kb/articles/q185/9/78.asp. To summarize: When Internet Explorer connects to a Web server resource that dynamically generates Word, Excel, or other Active Documents, Internet Explorer may send two GET requests for the resource. The second GET usually does not have session state information, temporary cookies, or authentication information that may have already been specified for the client. This bug can affect any local server (EXE) Active Document application hosted inside the Internet Explorer frame window. It occurs most frequently with ISAPI, ASP, or CGI applications that adjust the HTTP "Content Type" header to identify the installed application.

我呢,也碰到一个问题:我把它放在我自己的机器上(WIN2000)的时候,它并不给我下载或打开,只有把它放在另一台WEBSERVER上的时候才调试成功。
如果兄弟们有什么更好的招,别忘了告诉我呀。




打印本文 打印本文  关闭窗口 关闭窗口