将Excel电子表格作为网页返回 [英] Returning Excel Spreadsheet as web page

查看:75
本文介绍了将Excel电子表格作为网页返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我正在尝试返回Excel文件的内容。但是,我不想将请求重定向到Excel文件,因为它不在适当的位置。我试图将文件的内容写入响应,但遇到了麻烦。我的代码驻留在链接按钮'的点击事件的处理程序中


Response.ClearHeaders()

Response.ClearContent()

Response.ContentType =" application / vnd.ms-excel"

Response.AddHeader(" Content-Disposition"," attachment; filename =" + filename +" .XLS")

Response.WriteFile(路径)

Response.Flush()

Response.End()


这几乎可行。当我运行它时,我得到两个对话框,询问我是否要打开或保存filename.xls。如果我同时单击打开,Excel将打开(从浏览器外部),其中包含文件。当我拿出附件时在标题中指定,我只得到1个对话框但是,如果我点击打开,它最终会在我的浏览器中在Excel中显示我的网站的登录页面 - 我认为它失败了某种安全性检查或它被解释为当时未经验证的用户


我做错了什么?我不喜欢上面的代码我收到的两个对话框(我相信其中一个导致返回起始页面的行为相同,这并不是那么好)。并且,我更喜欢Excel打开嵌入在浏览器而不是独立模式(虽然,此时我会采取behvaior避免第二个对话框)


谢谢任何帮助!

Hello

I am trying to return the contents of an Excel file. However, I don''t want to just redirect the request to the Excel file since it does not reside in a location where that is appropriate. I am trying to write the contents of the file to the response but am having trouble. My code resides in the handler for a link-button''s click event

Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".XLS")
Response.WriteFile(path)
Response.Flush()
Response.End()

This almost works. When I run it, I get two dialog boxes asking if I want to open or save filename.xls. If I click ''Open'' on both, Excel opens up (external from the browser) with the file in it. When I take out the "attachment" designation in the header, I only get 1 dialog box but, if I click ''Open'', it ends up displaying the login page of my site in Excel within my browser --- I assume that it is failing some kind of security check or it is being interrpreted as an unauthenticated user at that point

What am I doing wrong? I don''t like the two dialog boxes I receive with the code above (and I am sure one of them is resulting in the same behavior of returning the start-page which isn''t so good). And, I would prefer that Excel open embedded in the browser instead of in stand-alone mode (although, at this point I would take either behvaior to avoid the second dialog box)

Thanks for any assistance!

推荐答案



你没有做错任何事;我所知道的唯一工作是改变你的代码,而不是做一个Content-Disposition(并通过重定向进行返工)。下面的示例代码生成双重打开。几周前,这个小组进行了讨论;我认为最后一个想法是,这是IE中的一个错误....


Scott


<%@ Page language =" ; C#" AutoEventWireup = QUOT假QUOT; %>

< script language =" C#" runat =" server">

public void Btn1_Click(object sender,System.EventArgs e)

{

string content ="你好" ;;

Response.Clear();

Response.ClearHeaders();

Response.Charset ="";

Response.ContentType =" application / csv";

Response.AddHeader(" Content-Disposition"," attachment; filename = HelloWorld.csv");

Response.AddHeader(" Content-Length",content.Length.ToString());

Response.Write(content);

回复.Flush();

Response.End();

}

< / script>

< ; html>

< body>

< form id =" Form1"方法= QUOT;交" runat =" server">

< asp:Button id =" Btn1" RUNAT = QUOT;服务器"文本= QUOT;按钮和QUOT; onclick =" Btn1_Click">< / asp:Button>

< / form>

< / body>

< ; / html>

< Ke ********* @newsgroups.nospam>在消息新闻中写道:1B ********************************** @ microsof t.com ...

您好,


我正在尝试返回Excel文件的内容。但是,我不想将请求重定向到Excel文件,因为它不在适当的位置。我试图将文件的内容写入响应,但遇到了麻烦。我的代码驻留在链接按钮的点击事件的处理程序中:

Response.ClearHeaders();

Response.ClearContent();

Response.ContentType =" application / vnd.ms-excel";

Response.AddHeader(" Content-Disposition"," attachment; filename =" + filename +" .XLS");

Response.WriteFile(path);

Response.Flush();

Response.End( );


这几乎可行。当我运行它时,我得到两个对话框,询问我是否要打开或保存filename.xls。如果我同时单击打开,Excel将打开(从浏览器外部),其中包含文件。当我拿出附件时在标题中指定,我只得到1个对话框但是,如果我点击打开,它最终会在我的浏览器中在Excel中显示我的网站的登录页面 - 我认为它失败了某种安全性检查或它被解释为当时未经身份验证的用户。


我做错了什么?我不喜欢上面的代码我收到的两个对话框(我相信其中一个导致返回起始页面的行为相同,这并不是那么好)。并且,我希望Excel在浏览器中打开而不是在独立模式下打开(虽然,此时我会采取behvaior来避免第二个对话框)。


感谢您的帮助!

You aren''t doing anything wrong; The only work around I know of, is to change your code and not do a Content-Disposition (and rework via a redirect). The sample code below generates the double open. There was a discussion in this group a few weeks ago; I think the last idea was that this is a bug in IE....

Scott

<%@ Page language="c#" AutoEventWireup="false" %>
<script language="C#" runat="server">
public void Btn1_Click(object sender, System.EventArgs e)
{
string content = "Hello";
Response.Clear();
Response.ClearHeaders();
Response.Charset = "";
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=HelloWorld.csv");
Response.AddHeader("Content-Length", content.Length.ToString());
Response.Write(content);
Response.Flush();
Response.End();
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
<Ke*********@newsgroups.nospam> wrote in message news:1B**********************************@microsof t.com...
Hello,

I am trying to return the contents of an Excel file. However, I don''t want to just redirect the request to the Excel file since it does not reside in a location where that is appropriate. I am trying to write the contents of the file to the response but am having trouble. My code resides in the handler for a link-button''s click event:

Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".XLS");
Response.WriteFile(path);
Response.Flush();
Response.End();

This almost works. When I run it, I get two dialog boxes asking if I want to open or save filename.xls. If I click ''Open'' on both, Excel opens up (external from the browser) with the file in it. When I take out the "attachment" designation in the header, I only get 1 dialog box but, if I click ''Open'', it ends up displaying the login page of my site in Excel within my browser --- I assume that it is failing some kind of security check or it is being interrpreted as an unauthenticated user at that point.

What am I doing wrong? I don''t like the two dialog boxes I receive with the code above (and I am sure one of them is resulting in the same behavior of returning the start-page which isn''t so good). And, I would prefer that Excel open embedded in the browser instead of in stand-alone mode (although, at this point I would take either behvaior to avoid the second dialog box).

Thanks for any assistance!


您好Keith,


从您的描述中,您在尝试显示时遇到了一些问题
a excel文件到某个asp.net网页对客户的响应,是吗?


1.至于第一个问题,这似乎是一个已知的行为我'以前也见过

。当我们将代码放在按钮的回发事件处理程序中,然后当页面返回时

,文件下载对话框将弹出两次。如果我将
代码放在page_load中,如果没有弹出窗口就可以正常运行

两次问题。你可能试图证实这一点,我想也许这可能是一个潜在的解决方法。您如何看待?


2.至于显示登录页面的问题而不是擅长,我会确认更多的东西:

1)你是否在asp.net web应用程序中使用FormsAuthentication。它是
似乎在访问excel文件时你会重定向到登录页面。


2)。如果1)是正确的,那么你是否在某些

excel文件上设置了授权保护,如果是这样的话,我想你可以尝试写一个特定的excel文件,这是

not受保护(可以由尚未登录的未经身份验证的用户访问

)。然后运行代码获取以查看问题是否是由于

表单身份验证。


请试一试,祝你好运!谢谢。


问候,


Steven Cheng

微软在线支持


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)


在ASP.NET上获取预览whidbey
< a rel =nofollowhref =http://msdn.microsoft.com/asp.net/whidbey/default.aspxtarget =_ blank> http://msdn.microsoft.com/asp.net/whidbey /default.aspx

Hi Keith,

From your description, you encountered some problems when trying to display
a excel file to the reponse of a certain asp.net web page to client, yes?

1. As for the first problem, this seems a known behavior I''ve also met
before. When we put the code in a button''s post back event handler, then
when the page return back, the file download dialog will popup twice. If I
put the the code in page_load instead, if will work ok without the popup
twice problem. You may have a try to confirm this, also I think this maybe
a potential workaround. How do you think ?

2. As for the problems that display "login page" rather than excel, I''d
like to confirm some further things:
1) Are you using FormsAuthentication in your asp.net web application. It
seems that you''re redirect to the login page when accessing the excel file.

2). If 1) is right, then have you set authorize protection on the certain
excel files, if so ,I think you can try write a certain excel file which is
not protected (can be accessecd by unauthenticated user who hasn''t login
yet). Then run the code a gain to see whether the problem is due to the
form authentication.

Please have a try and wish you good luck! Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx


嗨基思,


你有没有机会结账在我上次回复中提出的建议或者你有什么建议吗?b $ b你对这个问题有什么进一步的想法吗?如果您有任何不清楚的地方或者我们可以提供其他任何帮助,请随时在此处发布。谢谢。


问候,


Steven Cheng

微软在线支持


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)


在ASP.NET上获取预览whidbey
< a rel =nofollowhref =http://msdn.microsoft.com/asp.net/whidbey/default.aspxtarget =_ blank> http://msdn.microsoft.com/asp.net/whidbey /default.aspx

Hi Keith,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there''re anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx


这篇关于将Excel电子表格作为网页返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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