gridview 导出excel 迅雷下载时问题 急求解决方法!

wangkuixing 2010-07-28 12:39:14
在gridview导出excle时,若遇到迅雷下载就会出问题,下载的是.aspx页面,有时候还下载不了。

代码如下:
protected void Button_Save_Click(object sender, EventArgs e) //保存到Excel表中
{
GridView2.AllowPaging = false; //分页隐藏
GridView2.Columns[12].Visible = false; //删除列隐藏
GridView2.BottomPagerRow.Visible = false; //下面的分页设置隐藏
GridView2.DataBind();

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

Page page = new Page();
HtmlForm form = new HtmlForm();

GridView2.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();

page.Controls.Add(form);
form.Controls.Add(GridView2);

page.RenderControl(htw);
Response.Clear(); //清空输出流
Response.Buffer = true; //开启缓冲输出
Response.ContentType = "application/vnd.ms-excel";
string filename = DateTime.Now.ToString("d");//获取时间(作为文件名)
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");//设置存成的文件名字
Response.Charset = "GB2312"; //解决中文乱码的问题
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.ContentEncoding = System.Text.Encoding.UTF7;

Response.Write(sb.ToString());
Response.End();

GridView2.Columns[12].Visible = true;
GridView2.BottomPagerRow.Visible = true;
GridView2.AllowPaging = true;
GridView2.DataBind();

}
...全文
680 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangkuixing 2011-11-14
  • 打赏
  • 举报
回复
好久了,都忘了结贴了
冰封剑心 2010-08-14
  • 打赏
  • 举报
回复
  if (File.Exists(filepath))
{
string filename = strFileName;

System.IO.FileInfo file = new FileInfo(filepath);


FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);//file.Create();
BinaryReader br = new BinaryReader(fs);
if ((int)fs.Length==0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(\"文件不存在!!\")</script>");
return;
}
byte[] byteTemp = br.ReadBytes((int)fs.Length);
fs.Close();
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
Encoding code = Encoding.GetEncoding("gb2312");
response.ContentEncoding = code;
response.HeaderEncoding = code;
string SufName=string.Empty;
SufName=GetSufName(filename);
if (SufName == "tif")
{
response.AddHeader("Content-Type", "image/tiff");
}
else if (SufName == "txt")
{
response.AddHeader("Content-Type", "text/plain");

}
else if (SufName == "doc")
{
response.AddHeader("Content-Type", "application/msword");

}
else if (SufName == "rar")
{
response.AddHeader("Content-Type", "application/octet-stream");

}
else
{
response.AddHeader("Content-Type", "application/octet-stream");

}

if (Request.QueryString["LoadType"].ToString() == "download" )
{

response.AppendHeader("Content-Disposition",
"attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + "; size=" + byteTemp.Length.ToString());
}
else {
response.AppendHeader("Content-Disposition",
"inline; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + "; size=" + byteTemp.Length.ToString());
}
response.Flush();
response.BinaryWrite(byteTemp);
response.Flush();
response.End();
}
else
{
Response.Write("<script>alert('附件所对应的物理文件已经在应用程序外被删除!')</script>");
Response.Write("<script>window.history.back()</script>");
}


类似问题:我的只有在tif文件格式的时候出现 迅雷弹出. 目前使用的方法“不监视网站"。可以考虑直接在程序里面修改迅雷设置,如ie加载项里面去掉或者把烦人的迅雷屏蔽。

Adechen 2010-07-28
  • 打赏
  • 举报
回复
设置两次contextType是为什么呀
wwfgu00ing 2010-07-28
  • 打赏
  • 举报
回复

用文件流
/// <summary>
/// 指定要下载文件的虚拟路径及文件名
/// </summary>
/// <param name="FileName"></param>
public void downloadfile(string FileName)
{

//打开要下载的文件
System.IO.FileStream r = new System.IO.FileStream(Server.MapPath(FileName), System.IO.FileMode.Open);
//设置基本信息
Response.Buffer = false;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName));
Response.AddHeader("Content-Length", r.Length.ToString());


while (true)
{
//开辟缓冲区空间
byte[] buffer = new byte[1024];
//读取文件的数据
int leng = r.Read(buffer, 0, 1024);
if (leng == 0)//到文件尾,结束
break;
if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
Response.BinaryWrite(buffer);
else
{
//读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte[] b = new byte[leng];
for (int i = 0; i < leng; i++)
b[i] = buffer[i];
Response.BinaryWrite(b);
}
}
r.Close();//关闭下载文件
Response.End();//结束文件下载
}
YnSky 2010-07-28
  • 打赏
  • 举报
回复
在迅雷里有设置.不监视网页就好了,点击下载就不启动迅雷了
wangkuixing 2010-07-28
  • 打赏
  • 举报
回复
contexType是我多写了一次,完全可以去掉一句。
暖枫无敌 2010-07-28
  • 打赏
  • 举报
回复
这个好像跟IE浏览器的设置有点关系,我以前也遇到过,除了改变下设置外,我还真没找到解决办法,期待高手。
Devillyd 2010-07-28
  • 打赏
  • 举报
回复
遇到同样的问题,关注
myhope88 2010-07-28
  • 打赏
  • 举报
回复
一般来说,contenttype有设定,而且headers中设定attachement了,应该不会有问题吧

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧