将数据导出到经典ASP中的excel文件失败 [英] Export data to excel file from Classic ASP failing

查看:214
本文介绍了将数据导出到经典ASP中的excel文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将记录集导出到Excel中,但似乎在生产服务器上保持失败。但是,我的开发工作站似乎工作正常。我想知道我适合是一个服务器相关问题,但我有其他应用程序可以导出只是使用相同的确切代码,很好的类似的代码相同的设置。

 <%@ Language = VBScript%> 
<%Response.expires = -1%>
<%response.buffer = true%>
<%
今天
今天=_+替换(日期,/,)+_+替换(时间(),: )

Response.Charset =ANSI
Response.ContentType =application / octet-stream
Response.ContentType =application / vnd.ms-excel
Response.AddHeaderContent-Disposition,attachment; filename = List+ today +.xls
Response.ContentType =application / download

set Cnn = server.CreateObject(ADODB.connection)
Cnn.ConnectionString =应用程序(Cnn_ConnectionString)
Cnn.open

set rs1 = server.CreateObject(ADODB.Recordset )
SQLCollections =Sp_MysProc @ Param1 =& Session(var1)
rs1.open SQLCollections,cnn
%>
< html>
< body>
< table>
< tr>
< td> Number< / td>
< td> Name< / td>
< / tr>
<%if not rs.eof then
do while not rs.eof%>
< tr>
< td><%= rs(Number)%>< / td>
< td><%= rs(Name)%>< / td>
< / tr>
<%
rs.MoveNext
循环
rs.Close
set rs = Nothing
如果
%>
< / table>
< / body>
< / html>

再次,这可以从我的机器。但是,当我从制作中做到这一点,它给我以下消息:


Internet Explorer无法从www下载
MyFile.asp。 mydomain.com



Internet Explorer无法打开此网站的
。请求的网站
不可用或不能
找到。请稍后重试。


除了错误还有什么办法让它导出,而不是显示为HTML白色背景,没有行,就像一个真正的Excel文件会吗?



编辑
内容类型已根据Anthony的回答进行更正。



日期不是硬编码,允许每天创建多个文件,无需任何用户干预(用户请求)。



我已更新以删除If不是EOF。我注意到很多长时间的连接,也许应用程序周围有许多类型的问题。谢谢你的提示。此外,它仍然工作desipte没有记录集是按要求。



编辑2
我已经修复了eof的问题与不正确的列名称(oops!),它现在正确下载在我的电脑上从生产。我有Office 2007.但事情仍然不会在至少一台其他电脑上下载。这台其他电脑上有Office 2000。但是删除标题,并允许它溢出jsut它在所有机器上的HTML。



Might Office 2000有这样的问题吗?

解决方案

p>首先几个房子保存的东西。



内容类型3次的设置很少。只要坚持使用`application\vnd.ms-excel'一个。



而不是使用ANSI作为字符集使用Windows-1252。 p>

输出有多大?由于您正在缓冲,您可能会遇到ASP缓冲区默认最大值为4MB的IIS6。



关闭缓冲或弹出元数据库编辑器,并增加应用程序中的AspBufferingLimit值。



编辑



接下来要尝试的是在客户端安装 Fiddler



您安装了什么版本的MS办公室?


I'm trying to export a record set into Excel but it seems to keep failing on the production servers. However, it seems to work just fine on my development workstation. I'm wondering i fit's a server related issue but I have other apps that can export just fine using the same exact code, well similar code same set up.

<%@ Language=VBScript %>
<%Response.expires = -1%>
<%response.buffer = true%>
<%
     Dim today 
     today = "_" + Replace(Date,"/","") + "_" + Replace(Time(),":", "")

     Response.Charset = "ANSI"
     Response.ContentType = "application/octet-stream"
     Response.ContentType = "application/vnd.ms-excel"
     Response.AddHeader "Content-Disposition", "attachment; filename=List" + today + ".xls" 
     Response.ContentType = "application/download"

     set Cnn = server.CreateObject("ADODB.connection")
     Cnn.ConnectionString = Application("Cnn_ConnectionString")
     Cnn.open      

     set rs1 = server.CreateObject("ADODB.Recordset") 
     SQLCollections = "Sp_MysProc @Param1=" & Session("var1")
     rs1.open SQLCollections,cnn
%>
<html>
    <body>
        <table>
            <tr>
                <td>Number</td> 
                <td>Name</td> 
            </tr>
        <%if not rs.eof then
            do while not rs.eof %>
            <tr> 
                <td><%=rs("Number") %></td> 
                <td><%=rs("Name") %></td>   
            </tr>
        <%
            rs.MoveNext
            Loop
           rs.Close
           set rs = Nothing 
         End if        
        %>
        </table>
    </body>
</html>

Again, this works from my machine. But when I do it from production it gives me the following message:

Internet Explorer cannot download MyFile.asp from www.mydomain.com

Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

Beyond the error is there any way to make it export and not display as HTML with a white background and no lines, i.e. like a real Excel file would?

Edit: Content types have been corrected based on Anthony's answer.

The date is not hard coded to allow multiple files to be created daily with out any user intervention (user requested).

I've updated to remove the If Not EOF. I've been noticing a lot of long running connections, perhaps there are a number of these types of issues around the app. Thanks for the tip. Also it still works desipte there being no recordset which was as requested.

Edit 2 I've fixed on eof the issue with an improper column name (oops!) and it now downloads correctly on my computer from production. I have Office 2007. But the thing still will not download on at least one other computer. This other computer has Office 2000 on it. But removeing the headers and allowing it to spill out jsut the HTML it works on all machines.

Might Office 2000 have an issue with this sort of thing?

解决方案

First a couple of house keeping things.

There is little point setting the Content-Type 3 times. Just stick with the `application\vnd.ms-excel" one.

Rather than using "ANSI" as the character set use "Windows-1252".

How big is the output? Since you are buffering you may be hitting the ASP buffer default maximum of 4MB of IIS6.

Either turn off buffering or pop into metabase editor and increase the AspBufferingLimit value on your application.

Edit:

The next thing I would try is install Fiddler on my client and attempt the download. What do you see in fiddler when you attempt to download the file?

What version of MS office do you have installed?

这篇关于将数据导出到经典ASP中的excel文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆